展平列表中的嵌套列表

时间:2020-12-03 19:46:02

I have a list with nested lists inside.

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

LIST2 <- list(list("USA","WY","TX","AZ","Canada", "CA", "NY", 'Russia', 'NY'), 
             list(c("USA","Canada","CA","WY", 'China', 'AZ', 'AZ', 'AZ', 'WY')), 
             list(c("USA","Australia","CA","AR", 'AZ', 'WY', 'New Zealand', 'Japan', 'Japan', 'NJ')),
             list(list('Australia', 'Australia', 'Japan', 'Malaysia' )),
             list(c('USA', 'Australia', 'Japan', 'Malaysia' )))

I would like to flatten somehow the 1st and 4th list so they are same form as the rest of them. Is this possible?

我想以某种方式压扁第一和第四列表,以便它们与其余部分相同。这可能吗?

3 个解决方案

#1


6  

Loop through the list, unlist recursively, then return as a list:

循环遍历列表,递归取消列表,然后以列表形式返回:

lapply(LIST2, function(i) list(unlist(i, recursive = TRUE)))

#2


6  

We could use rapply

我们可以使用rapply

lapply(LIST2, rapply, f = c)

#3


3  

this should do it for you:

这应该为你做:

LIST2 <- lapply(LIST2, unlist)

#1


6  

Loop through the list, unlist recursively, then return as a list:

循环遍历列表,递归取消列表,然后以列表形式返回:

lapply(LIST2, function(i) list(unlist(i, recursive = TRUE)))

#2


6  

We could use rapply

我们可以使用rapply

lapply(LIST2, rapply, f = c)

#3


3  

this should do it for you:

这应该为你做:

LIST2 <- lapply(LIST2, unlist)