【问题标题】:Create new lists from a list of data frames, based on specific conditions根据特定条件从数据框列表创建新列表
【发布时间】:2018-01-19 03:25:07
【问题描述】:

我有一个数据框列表,如何根据具体情况将它们分组到不同的列表中?

例如:

mylist = list(matrix(rep(0,9),nrow=3),matrix(rep(c(0,1),9),nrow=3), matrix(rep(1,9),nrow=3), matrix(rep(c(0,1),9),nrow=3, byrow=T))

我正在尝试将这些数据帧分组到新列表中,具体取决于它们的最大 rowSums 和 colSums。例如,这些是条件:

cond = data.frame(group = c(1,2,3), maxrowsums = c(0, 3, 3), maxcolsums = c(0, 2, 3))

基于这些条件,mylist[[1]] 属于第 1 组,mylist[[2]] 属于第 2 组,mylist[[3]] 和 mylist[[4]] 都属于第 3 组:

lapply(mylist, FUN=function(x) max(rowSums(x)))
lapply(mylist, FUN=function(x) max(colSums(x)))

我正在努力解决将列表拆分为列表子集的部分,特别是,我如何根据这些条件找到索引并将列表子集为列表列表?

【问题讨论】:

  • 请用预期的输出更新您的帖子。

标签: r list


【解决方案1】:

您可以根据您想要的任何条件使用真/假向量来对您的列表进行子集化吗?

      mylist = list(matrix(rep(0,9),nrow=3),matrix(rep(c(0,1),9),nrow=3), matrix(rep(1,9),nrow=3), matrix(rep(c(0,1),9),nrow=3, byrow=T))

       # subset where rowsums are equal to three 
subsettedlist <- mylist[ unlist( lapply(mylist, FUN=function(x) max(rowSums(x))==3 ) ) ]

【讨论】:

  • 这两个答案都有很大帮助。这可用于遍历数据框并创建一个新列表: newlist = list(); for (i in 1:nrow(cond)) ...newlist[[length(newlist)+1]] = subsettedlist
【解决方案2】:

这是一个tidyverse 解决方案。我们可以将数据框列表放入列表列中。

我们使用 match 获得每个数据帧的最大行/列总和。然后我们在maxrowsums 和maxcolsums 相等的cond 数据帧上加入它。

library('tidyverse')

mylist %>%
  enframe(name = 'df_id', value = 'df') %>%
  mutate(
    maxrowsums = map_dbl(df, ~max(rowSums(.))),
    maxcolsums = map_dbl(df, ~max(colSums(.))),) %>%
  left_join(cond)
#> Joining, by = c("maxrowsums", "maxcolsums")
#> # A tibble: 4 x 5
#>   df_id            df maxrowsums maxcolsums group
#>   <int>        <list>      <dbl>      <dbl> <dbl>
#> 1     1 <dbl [3 x 3]>          0          0     1
#> 2     2 <dbl [3 x 6]>          3          2     2
#> 3     3 <dbl [3 x 3]>          3          3     3
#> 4     4 <dbl [3 x 6]>          3          3     3

如果你想要一个基本的R解决方案,你只需要根据maxrowsums和maxcolsums过滤cond数据框。

lapply(mylist, FUN=function(x) {
  maxrowsums <- max(rowSums(x))
  maxcolsums <- max(colSums(x))
  cond$group[maxrowsums == cond$maxrowsums & maxcolsums == cond$maxcolsums]
})
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 
#> [[4]]
#> [1] 3

【讨论】:

    【解决方案3】:

    数据:

    mylist = list(data.frame(matrix(rep(0,9),nrow=3)), data.frame(matrix(rep(c(0,1),9),nrow=3)), data.frame(matrix(rep(1,9),nrow=3)), data.frame(matrix(rep(c(0,1),9),nrow=3, byrow=T)))
    

    条件表:

    condition_table = data.frame(group = c(1,2,3), maxrowsums = c(0, 3, 3), maxcolsums = c(0, 2, 3))
    

    制作群组功能:

    make_groups <- function(mylist, condition_table) {
        tmp_list <- list()
        groupings <- list()
        for(group in 1:nrow(condition_table)) {
            max_row_cond <- condition_table[group,c('maxrowsums')]
            max_col_cond <- condition_table[group,c('maxcolsums')]
            tmp_list[group] <- list(sapply(mylist, function(x) { if(max(rowSums(x))==max_row_cond && max(colSums(x))==max_col_cond) { return(x) } }))
            groupings[group] <- list(Filter(Negate(is.null), tmp_list[[group]]))
            }
        names(groupings) <- sapply(seq(1:3), function(x){ paste('Group: ', x, collapse='', sep='')})
        return(groupings)
    }
    

    用法:

    make_groups(mylist, condition_table)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      相关资源
      最近更新 更多