考虑以下嵌套列表:
vars <- c("A", "B")
lapply(1:2, function(x) combn(vars, x, simplify=FALSE))
[[1]]
[[1]][[1]]
[1] "A"
[[1]][[2]]
[1] "B"
[[2]]
[[2]][[1]]
[1] "A" "B"
我想将结果(可能通过使用 lapply 以外的其他功能)转换为以下内容:
[[1]]
[1] "A"
[[2]]
[1] "B"
[[3]]
[1] "A" "B"
答案 0 :(得分:2)
好像你只想删除一级嵌套。您可以使用unlist(..., recursive=FALSE)
vars <- c("A", "B")
x <- lapply(1:2, function(x) combn(vars, x, simplify=FALSE))
unlist(x, recursive=FALSE)
# [[1]]
# [1] "A"
#
# [[2]]
# [1] "B"
#
# [[3]]
# [1] "A" "B"