我有一个嵌套的列表.嵌套列表的"中心"是一个包含n个整数的向量.我需要计算每个嵌套列表中有多少个整数,然后取消上面的一个级别,以获得这些计数的向量(即,不是嵌套中心的list(0, 1:5, 0, 0, 1:3),而是c(0, 5, 0, 0, 3)).

这似乎相对简单——我能够用rapply完成第一部分,即将list(0, 1:5, 0, 0, 1:3)转换为list(0, 5, 0, 0, 3).My specific question我需要的帮助是如何将最里面的列表取消到向量(而不是list(0, 5, 0, 0, 3)我想要c(0, 5, 0, 0, 3))

我已经搜索并try 了各种各样的applylapplyunlist方法,但没有一种是完全正确的,因为它们的目标是最里面的列表.因为我想取消列出的列表是倒数第二个元素,所以我正在努力找到一种优雅地完成这一点的方法.

在下面的示例数据中,我可以通过两种方式获得所需的结果:多个lapply函数或一个for循环.然而,我的实际数据包含更多的列表和数百万个数据点,因此这些可能不是有效的选项.

下面是(1)样本数据,(2)我try 过的,以及(3)具有所需 struct 的样本数据.

样本数据

have_list <- list(scenario1 = list(method1 = list(place1 = list(0, 1:5, 0, 0, 1:3),
                                                  place2 = list(1:2, 0, 1:10, 0, 0),
                                                  place3 = list(0:19, 0, 0, 0, 0),
                                                  place4 = list(1:100, 0, 0, 1:4, 0)),
                                   method2 = list(place1 = list(1:5, 1:5, 0, 0, 1:3),
                                                  place2 = list(0, 0, 1:5, 0, 0),
                                                  place3 = list(0:19, 0, 1:7, 0, 0),
                                                  place4 = list(1:22, 0, 0, 1:4, 0)),
                                   method3 = list(place1 = list(0, 1:2, 1:6, 0, 1:3),
                                                  place2 = list(1:2, 0, 1:6, 1:4, 0),
                                                  place3 = list(0:19, 0, 0, 0, 1:2),
                                                  place4 = list(1:12, 0, 0, 1:12, 0))),
                  scenario2 = list(method1 = list(place1 = list(0, 1:5, 0, 0, 1:3),
                                                  place2 = list(1:2, 0, 1:10, 0, 0),
                                                  place3 = list(0:19, 0, 0, 0, 0),
                                                  place4 = list(1:100, 0, 0, 1:4, 0)),
                                   method2 = list(place1 = list(1:5, 1:5, 0, 0, 1:3),
                                                  place2 = list(0, 0, 1:5, 0, 0),
                                                  place3 = list(0:19, 0, 1:7, 0, 0),
                                                  place4 = list(1:22, 0, 0, 1:4, 0)),
                                   method3 = list(place1 = list(0, 1:2, 1:6, 0, 1:3),
                                                  place2 = list(1:2, 0, 1:6, 1:4, 0),
                                                  place3 = list(0:19, 0, 0, 0, 1:2),
                                                  place4 = list(1:12, 0, 0, 1:12, 0))))

我试过的

因此,我访问了一些问题:

# Get number of integers in each nested list 
lengths <- rapply(have_list, function(x) unlist(length(x)), how = "list") # this works fine

#' Each count is currently still in its own list of length 1,
#' Convert each count to vector
#' In the "middle" the nested list:
    # I have list(0, 5, 0, 0, 3) 
    # I want c(0, 5, 0, 0, 3)

# Attempts to unlist the counts
# Unlist the counts
test1 <- rapply(lengths, unlist, how = "list") # doesn't work
test2 <- unlist(lengths, recursive = FALSE) # doesn't work
test3 <- lapply(lengths, function(x) lapply(x, unlist)) # doesnt work
test4 <- lapply(lengths, function(x) lapply(x, unlist, recursive = FALSE)) # doesnt work 
test5 <- rapply(have_list, function(x) unlist(length(x)), how = "list")  #doesnt work
test6 <- rapply(have_list, function(x) unlist(length(x)), how = "unlist")  #doesnt work

我想要的数据 struct

# This works on test data but is impractical for real data
want_list <- lapply(lengths, function(w) lapply(w, function(x) lapply(x, unlist)))

# or

want_list <- lengths 

## for loops work but is not practical

for (i in 1:length(lengths)){
  for (j in 1:length(lengths[[i]])){
    for (k in 1:length(lengths[[i]][[j]])){
      want_list[[i]][[j]][[k]] <- unlist(lengths[[i]][[j]][[k]])
    }
  }
}

推荐答案

这可以通过使用递归来实现.一个简单的递归将是:

my_fun <- function(x) if(is.list(x[[1]])) lapply(x, my_fun) else lengths(x)

out <- my_fun(have_list)

identical(out, want_list)
[1] TRUE

R相关问答推荐

从R导出全局环境中的所有sf(numrames)对象

当月份额减go 当月份额

如何在R中合并两个基准点?

使用sf或terra的LINESTRAING的累积长度

在组中添加值增加和减少的行

即使硬币没有被抛出,也要保持对其的跟踪

将饼图插入条形图

如何在R中描绘#符号?

使用rvest从多个页面抓取时避免404错误

如何指定我的函数应该查找哪个引用表?

在R中,如何将误差条放置在堆叠的每个条上?

在R中使用列表(作为tibble列)进行向量化?

有没有办法将不等长的列表转换为R中的数据帧

如何判断代码是否在R Markdown(RMD)上下文中交互运行?

使用列名和r中的前缀 Select 列的CREATE函数

如何在R中的两列以上使用联合(&U)?

将Geojson保存为R中的shapefile

用LOOCV进行K近邻问题

将矩阵中特定行的双精度值添加到下一行中

仅在Quarto中运行块的一部分