【问题标题】:passing data through a sapply in R通过 R 中的 sapply 传递数据
【发布时间】:2022-01-24 02:01:32
【问题描述】:

我正在使用 R。我有 120 个如下所示的文档:

chair   table  hill  block  chain  ball  money house

  2       4     5      6      7      2     4     5 
  1       3     6      1      8      3     9     1
  2       1     1      6      1      8     2     3
  6       4     5      4      2      5     8     4
  5       5     5      5      3      2     6     7 

我创建了一个函数:

myFunc <- function(x) c(mean = mean(x), n = length(x),
                        SD = sd(x))

然后,我使用sapply 函数并将结果转换为数据框。

lol3 <- sapply(mydat, myFunc)

lol4 <- as.data.frame(lol3)

如何通过我所有的 120 个文档并获得相同的输出?

【问题讨论】:

  • 文档以哪种形式保存?制表符分隔? CSV?
  • 是 CSV 文件

标签: r


【解决方案1】:

您可以使用类似这样的方法将数据读入 R(假设为 .csv 文件),这会将 120 个数据帧放入一个列表中,dfs

temp <- list.files(pattern="*.csv", 
                   full.names=TRUE)
dfs <- lapply(temp, read.csv)

然后,您可以使用purrr 将您的函数应用于每个数据帧中的所有列。然后我们可以使用set_names 为列表中的每个数据框命名。

library(tidyverse)

output <-
  purrr::map(dfs, function(x)
    as.data.frame(map(x, myFunc))) %>%
  purrr::set_names(., paste0("results-", seq(1, length(output), 1)))

然后,如果您想将每个数据帧输出为 .csv,那么您可以这样做(尽管您可能想要更改文件目录):

purrr::iwalk(output, function(dat, name)
  write.csv(dat, file = paste0("./", name, ".csv")))

或者,如果您想坚持使用apply 系列,则可以使用sapplylapply 的组合,这会产生相同的输出。

lapply(dfs, FUN = function(x) as.data.frame(sapply(x, myFunc)))

输出

[[1]]
        chair    table     hill
mean 5.707284 2.016096 4.632898
n    5.000000 5.000000 5.000000
SD   3.037238 2.609171 2.891649

[[2]]
        chair    table     hill
mean 4.972276 3.522378 2.779039
n    5.000000 5.000000 5.000000
SD   2.309736 2.402731 1.805293

[[3]]
        chair    table     hill
mean 4.614903 2.994203 3.573117
n    5.000000 5.000000 5.000000
SD   2.341367 2.250936 2.388503

[[4]]
        chair    table     hill
mean 3.962970 5.326495 5.289796
n    5.000000 5.000000 5.000000
SD   3.454892 3.250463 2.261613

数据和功能

dfs <-
  list(
    structure(
      list(
        chair = c(
          8.5190371570643,
          7.96348396944813,
          0.909618176054209,
          6.22358219046146,
          4.92070004786365
        ),
        table = c(
          6.57108870637603,
          1.24985219235532,
          0.31808012444526,
          1.61383197060786,
          0.327624693978578
        ),
        hill = c(
          6.09723530360498,
          4.0752498563379,
          0.514291892526671,
          8.34481998276897,
          4.13289327942766
        )
      ),
      class = "data.frame",
      row.names = c(NA,-5L)
    ),
    structure(
      list(
        chair = c(
          6.7650549269747,
          3.03855412406847,
          4.73554673418403,
          7.83120877831243,
          2.49101754790172
        ),
        table = c(
          0.390065581072122,
          4.98121203482151,
          2.45721989544109,
          6.66204453259706,
          3.12134596821852
        ),
        hill = c(
          1.91045304620638,
          4.58099147421308,
          0.0588874609675258,
          3.53219888708554,
          3.81266354466788
        )
      ),
      class = "data.frame",
      row.names = c(NA,-5L)
    ),
    structure(
      list(
        chair = c(
          6.3361757278908,
          6.55107694189064,
          0.718950896523893,
          4.66502936370671,
          4.80328303738497
        ),
        table = c(
          3.64353043888696,
          0.393067884491757,
          5.97248072689399,
          1.12406565248966,
          3.83787051541731
        ),
        hill = c(
          2.51783188269474,
          1.69069789093919,
          2.80711475084536,
          3.11010800697841,
          7.73983212606981
        )
      ),
      class = "data.frame",
      row.names = c(NA,-5L)
    ),
    structure(
      list(
        chair = c(
          1.01416687178425,
          8.35876755136997,
          6.14014947647229,
          4.20261403801851,
          0.0991506285499781
        ),
        table = c(
          5.58780367858708,
          6.96770576946437,
          8.87186487391591,
          0.143813505303115,
          5.06128515000455
        ),
        hill = c(
          5.23900085524656,
          8.74273954122327,
          5.80283471778966,
          2.78146772808395,
          3.8829374271445
        )
      ),
      class = "data.frame",
      row.names = c(NA,-5L)
    )
  )

myFunc <- function (x)
  c(mean = mean(x),
    n = length(x),
    SD = sd(x))

【讨论】:

  • 如何从数据框列表中单独取出每个数据框? @andrewgillreath-棕色
  • @Ajrhmamd 想要在全球环境中拥有每个数据框吗?还是要将每个保存为 csv?
  • 我想将每个文件保存为 csv 文件,作为单独的文件。 @Andrew Gillreath-Brown
  • @Ajrhmamd 我已经更新了答案,展示了如何命名每个数据帧,然后为每个数据帧输出 csv 文件。
【解决方案2】:

您需要读入文档的路径。我曾经写过medium article about path handling functions in R。第4点是关于如何递归列出文件夹中的文件。

# let's say your files have all the ending .txt
paths <- list.files("/to/dir_containing_all_the_files", 
                    pattern="\\.txt", 
                    full.names=TRUE, # give absolute paths
                    recursive=TRUE)  # search also in subfolders for files

# you read the correct file-read functions:
# let's assume - like your input - they are TAB (?) separated
dfs <- lapply(paths, function(path) read.delim(path, header=TRUE, sep="\t"))

# in case of csv files:
dfs <- lapply(paths, function(path) read.csv(path, header=TRUE))

# you have to take a single file and adjust the read.delim function correctly
# test it for some files until it works!

# the dfs should contain each of the data frames which were read-in.
# you know how you proceeded for a single data frame -
# just write a function for a single data frame:
process_df <- function(df) sapply(df, myFunc)

# use this to s/lapply over the dfs:
process_dfs <- function(dfs) sapply(dfs, process_df)

# then you call:
result <- process_dfs(dfs)
# and see how it is structured:
str(result)

或者你通过一个路径的所有过程,然后循环它:

process_path <- function(path) {
  df <- read.delim(path, header=TRUE, sep="\t")
  sapply(df, myFunc)
}

# finally, you can write:
process_paths <- function(paths) sapply(paths, process_path)

# and call:
process_paths(paths)

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 2017-04-18
    • 2020-03-07
    • 2021-09-30
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多