【问题标题】:Collapsing one list of dataframes and combining with another list of dataframes in R折叠一个数据框列表并与 R 中的另一个数据框列表组合
【发布时间】:2021-04-11 22:05:17
【问题描述】:

如何在 R 中处理两个具有多个数据帧的列表?示例数据:

set.seed(1)
set1 <- data.frame(NAME = paste("row_", 1:10, sep = ""),
                 SYMBOL = paste(c(sample(LETTERS, 10))),
                 SIGNIFICANT = sample(c("yes", "no"), 10, replace = TRUE))
set2 <- data.frame(NAME = paste("row_", 1:10, sep = ""),
               SYMBOL = paste(c(sample(LETTERS, 10))),
               SIGNIFICANT = sample(c("yes", "no"), 10, replace = TRUE))
set3 <- data.frame(NAME = paste("row_", 1:10, sep = ""),
                  SYMBOL = paste(c(sample(LETTERS, 10))),
                  SIGNIFICANT = sample(c("yes", "no"), 10, replace = TRUE))
set4 <- data.frame(NAME = paste("row_", 1:10, sep = ""),
                 SYMBOL = paste(c(sample(LETTERS, 10))),
                 SIGNIFICANT = sample(c("yes", "no"), 10, replace = TRUE))
files <- list(set1, set2, set3, set4)
names(files) <- paste("Set", 1:4, sep = "")
reports <- list(data.frame(SETS = c("Set1", "Set3"),
                        STATISTIC = runif(2)),
             data.frame(SETS = c("Set2", "Set4"),
                        STATISTIC = runif(2)))
names(reports) <- c("Report1", "Report2")

files 是一个列表,其中包含来自分析的许多元数据数据帧。

> files$Set1
     NAME SYMBOL SIGNIFICANT
1   row_1      Y          no
2   row_2      D          no
3   row_3      G          no
4   row_4      A         yes
5   row_5      B         yes
6   row_6      K         yes
7   row_7      N         yes
8   row_8      R         yes
9   row_9      W         yes
10 row_10      J         yes

reports 也是一个包含 2 个数据帧的列表,其中包含来自双向分析和相关统计数据的主要输出。

> reports$Report1
  SETS STATISTIC
1 Set1 0.4100841
2 Set3 0.8108702

请注意,files 列表中数据框的名称与reports 列表中数据框的第 2 列相对应。

我希望以特定方式折叠这些files 元数据。如果files$Set1$SIGNIFICANT == 'yes',我想将相应的SYMBOL 附加到逗号分隔的字符串中。然后,我想将该字符串附加到reports 内的相应集合中。因此,我想要的输出如下:

> head(reports$Report1)
  SETS STATISTIC              SYMBOL
1 Set1 0.4100841 A, V, K, N, R, W, J
2 Set3 0.8108702          F, S, J, V

同样适用于Report2

对于这个例子来说手动操作很容易,但在我的实际项目中,length(files)=600

我正在尝试通过 for 循环解析它,但一直遇到错误。这是我当前的迭代

output <- data.frame()
for(i in 1:length(files)){
  for(j in 1:nrow(files[[i]])){
    if(files[j, 3] == "Yes"){
      output[i, 1]=i;
      output[i, 2]=paste0(i[,2], collapse = ", ")
    }
  }
}

还有我当前的错误:

Error in i[[j, 3]] : incorrect number of subscripts

我已经使用 R 大约 4 年了,如果我知道一件事,那就是人们经常避免像瘟疫这样的循环。我知道applylapply 等的一些变化可能会让生活变得轻松。

【问题讨论】:

    标签: r list dataframe sorting


    【解决方案1】:

    这是类似于 MrFlick 的解决方案,但使用 subsetsetNames 和不同的 lapply 调用,可能更容易阅读:

    # get the characters with SIGNIFICANT equal to "yes"
    all_symbs <- lapply(files, subset, SIGNIFICANT == "yes", SYMBOL, TRUE)
    # create one data.frame with the above after concatenating
    all_files <- setNames(stack(lapply(all_symbs, paste0, collapse = ", ")),
                          c("SYMBOL","SETS"))
    # merge with reports
    res <- lapply(reports, merge, y = all_files)
    # the result
    res
    #R> $Report1
    #R>   SETS STATISTIC              SYMBOL
    #R> 1 Set1 0.4100841 A, B, K, N, R, W, J
    #R> 2 Set3 0.8108702          F, S, J, V
    #R> 
    #R> $Report2
    #R>   SETS STATISTIC           SYMBOL
    #R> 1 Set2 0.6049333             B, F
    #R> 2 Set4 0.6547239 W, Z, H, Q, D, M
    

    您可以通过创建一个匿名函数而不是两个lapply(files, ...)lapply(all_symbs, ...) 调用来摆脱lapply 调用之一。

    【讨论】:

      【解决方案2】:

      您可以使用sapply 迭代每个数据帧中的files 列表,仅保留SIGNIFICANT = 'yes' 值并将它们折叠成一个字符串。

      data <- stack(sapply(files,function(x) toString(x$SYMBOL[x$SIGNIFICANT=='yes'])))
      
      data
      #               values  ind
      #1 A, B, K, N, R, W, J Set1
      #2                B, F Set2
      #3          F, S, J, V Set3
      #4    W, Z, H, Q, D, M Set4
      

      然后您可以在merge datareports 中的每个dataframe

      result <- lapply(reports, function(x) merge(x,data, by.x = 'SETS', by.y = 'ind'))
      result
      
      #$Report1
      #  SETS STATISTIC              values
      #1 Set1 0.4100841 A, B, K, N, R, W, J
      #2 Set3 0.8108702          F, S, J, V
      
      #$Report2
      #  SETS STATISTIC           values
      #1 Set2 0.6049333             B, F
      #2 Set4 0.6547239 W, Z, H, Q, D, M
      

      【讨论】:

        【解决方案3】:

        我认为您可以分两步完成此操作,首先创建一个 data.frame,其中包含每个集合的重要符号。这里

        sigset <- stack(lapply(files, function(x) paste(x$SYMBOL[x$SIGNIFICANT=="yes"], collapse=", ")))
        names(sigset) <- c("SYMBOL","SETS")
        

        我们使用lapply()来迭代文件列表,提取重要的符号可以将它们组合在一起,然后我们将列表堆叠到一个data.frame中以使其更易于使用。我们更改名称,以便它可以更轻松地合并列名称。然后我们可以将此列表与每个报告合并

        output <- lapply(reports, function(x) merge(x, sigset))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-20
          • 1970-01-01
          • 2017-10-21
          相关资源
          最近更新 更多