如果元素在R中是多维的,如何展平所有(嵌套)元素?
假设我有以下数据:如果元素在R中是多维的,如何展平所有(嵌套)元素?,r,list,R,List,假设我有以下数据: data <- list(arr = c(2, 2), ab = list(x = c(4, 4), e = list(f = c(1, 2), ff = 1:3, dd = data.frame(c = 1:3, d = 2:4)))) 我的尝试: > list(arr = c(2, 2), ab.x = c(4, 4), ab.e.f = c(1, 2), + ab.e.ff = c(1, 2, 3)
data <- list(arr = c(2, 2), ab = list(x = c(4, 4),
e = list(f = c(1, 2), ff = 1:3, dd = data.frame(c = 1:3, d = 2:4))))
我的尝试:
> list(arr = c(2, 2), ab.x = c(4, 4), ab.e.f = c(1, 2),
+ ab.e.ff = c(1, 2, 3), ab.e.dd.c = 1:3, ab.e.dd.d = 2:4)
$arr
[1] 2 2
$ab.x
[1] 4 4
$ab.e.f
[1] 1 2
$ab.e.ff
[1] 1 2 3
$ab.e.dd.c
[1] 1 2 3
$ab.e.dd.d
[1] 2 3 4
(一)
(二)
继续我相信这肯定会有帮助-rlist::list.flatten(数据)
,这里已经解释过的方法我相信这肯定会有帮助-rlist::list.flatten(数据)
,这里已经解释过的方法
unlist(data, recursive = FALSE)
continue <- TRUE
lst <- list()
nr <- 1
while(continue){
lst[[nr]] <- data
data <- unlist(lst[[nr]], recursive = FALSE)
continue <- !identical(lst[[nr]], data)
nr <- nr + 1
}
lst[nr - 2]