R - 通过嵌套列表的参数子集列表

R - Subsetting list by paramaters of nested list

我有一个包含嵌套列表的列表 list_tot

我想根据指定的参数对 select 的特定子集进行子集化或创建一个新列表,详情如下:

List_1 <- list(a = matrix(5,2), b = matrix(5,7), c = matrix(5,9), d = matrix(5,3))
List_2 <- list(a = matrix(7,3), b = matrix(7,7), c = matrix(7,1), d = matrix(7,9))
List_3 <- list(a = matrix(5,2), b = matrix(5,7), c = matrix(5,9), d = matrix(5,3))
List_4 <- list(a = matrix(5,2), b = matrix(5,7), c = matrix(5,9), d = matrix(5,3))
List_5 <- list(a = matrix(5,2), b = matrix(5,7), c = matrix(5,9), d = matrix(5,3))

List_tot <- list(List_1, List_2, List_3, List_4, List_5)

上面写着:

[[1]]

[[1]]$a
     [,1]
[1,]    5
[2,]    5

[[1]]$b
     [,1]
[1,]    5
[2,]    5
[3,]    5
[4,]    5
[5,]    5
[6,]    5
[7,]    5

[[1]]$c
      [,1]
 [1,]    5
 [2,]    5
 [3,]    5
 [4,]    5
 [5,]    5
 [6,]    5
 [7,]    5
 [8,]    5
 [9,]    5

[[1]]$d
     [,1]
[1,]    5
[2,]    5
[3,]    5


[[2]]
[[2]]$a
     [,1]
[1,]    7
[2,]    7
[3,]    7

[[2]]$b
     [,1]
[1,]    7
[2,]    7
[3,]    7
[4,]    7
[5,]    7
[6,]    7
[7,]    7

[[2]]$c
     [,1]
[1,]    7
...etc

我要select:

  1. 仅限每个嵌套列表 select list/matrix a、c 和 d。
  2. 对于每个嵌套列表 select 两个 lists/matrices 具有最高行数。

所以 New_List_tot 的输出为:

上面写着:

[[1]]

[[1]]$c
      [,1]
 [1,]    5
 [2,]    5
 [3,]    5
 [4,]    5
 [5,]    5
 [6,]    5
 [7,]    5
 [8,]    5
 [9,]    5

[[1]]$d
     [,1]
[1,]    5
[2,]    5
[3,]    5
etc...

任何帮助都会有所帮助。我所有的尝试,尝试使用 plyer 和 dplyr,但没有成功,而且非常卡住。

我们可以使用base R来做到这一点。不需要包

lapply(List_tot, `[`, c("a", "c", "d"))

或使用匿名函数

lapply(List_tot, function(x) x[c("a", "c", "d")])

如果我们需要前 2 个,order 行数(lengths 因为这些是单列 matrix,所以行数等于总行数的元素,获取行数的有序向量的 nameshead 并使用它来提取内部列表元素

lapply(List_tot, function(x) {
      x1 <- x[c("a", "c", "d")]
      v1 <- lengths(x1)
      x1[head(names(v1)[order(-v1)], 2)]
    })

在基础 R 中你可以这样做:

lapply(List_tot, 
   function(x) (y<-x[c("a", "c", "d")])[order(sapply(y, nrow), decreasing  = TRUE)[1:2]])

[[1]]
[[1]]$c
      [,1]
 [1,]    5
 [2,]    5
 [3,]    5
 [4,]    5
 [5,]    5
 [6,]    5
 [7,]    5
 [8,]    5
 [9,]    5

[[1]]$d
     [,1]
[1,]    5
[2,]    5
[3,]    5
 etc

更新

第二个目标,你可以试试

lapply(
  List_tot,
  function(lst) {
    head(lst[c("a", "c", "d")][order(-sapply(lst[c("a", "c", "d")], nrow))], 2)
  }
)

这给出了

[[1]]
[[1]]$c
      [,1]
 [1,]    5
 [2,]    5
 [3,]    5
 [4,]    5
 [5,]    5
 [6,]    5
 [7,]    5
 [8,]    5
 [9,]    5

[[1]]$d
     [,1]
[1,]    5
[2,]    5
[3,]    5


[[2]]
[[2]]$d
      [,1]
 [1,]    7
 [2,]    7
 [3,]    7
 [4,]    7
 [5,]    7
 [6,]    7
 [7,]    7
 [8,]    7
 [9,]    7

[[2]]$a
     [,1]
[1,]    7
[2,]    7
[3,]    7


[[3]]
[[3]]$c
      [,1]
 [1,]    5
 [2,]    5
 [3,]    5
 [4,]    5
 [5,]    5
 [6,]    5
 [7,]    5
 [8,]    5
 [9,]    5

[[3]]$d
     [,1]
[1,]    5
[2,]    5
[3,]    5


[[4]]
[[4]]$c
      [,1]
 [1,]    5
 [2,]    5
 [3,]    5
 [4,]    5
 [5,]    5
 [6,]    5
 [7,]    5
 [8,]    5
 [9,]    5

[[4]]$d
     [,1]
[1,]    5
[2,]    5
[3,]    5


[[5]]
[[5]]$c
      [,1]
 [1,]    5
 [2,]    5
 [3,]    5
 [4,]    5
 [5,]    5
 [6,]    5
 [7,]    5
 [8,]    5
 [9,]    5

[[5]]$d
     [,1]
[1,]    5
[2,]    5
[3,]    5

  • 对于您的第一个目标“仅针对每个嵌套列表 select list/matrix a、c 和 d。”
lapply(List_tot, `[`, c("a", "c", "d"))
  • 对于您的第二个目标“对于每个嵌套列表 select lists/matrices 具有最高行数。”
Map(`[`, List_tot, max.col(t(sapply(List_tot, lengths))))

@akrun 对第 1 步有一个清晰的答案,按列过滤。对于第 2 步,您可以尝试按行数过滤

library(magrittr)
List_colfilter <- lapply(List_tot, function(i)i[c("a","c","d")])

longestlist <- function(l){
  maxr <- lapply(l,nrow) %>% unlist %>% max
  l2 <- lapply(l, function(x) if(nrow(x)==maxr) x else NA)
  for (n in names(l)){
    if (is.na(l2[n])){
      l2[n] <- NULL
    }
  }
  return(l2)
}
List_longfilter <- lapply(List_colfilter, longestlist)