假设我有N个都具有相同列名的列表。我想合并这些,以便得到具有相同列的结果列表,但现在包含所有N个列表中的条目。这是显示我想要的MWE:
ls<-list()
ls[[1]]<-list("a"=1,
"b"=2)
ls[[2]]<-list("a"=3,
"b"=4)
#how to write a one-liner that produces lsTotal, which is the union of ls[[1]] and ls[[2]]?
lsTotal<-list("a"=c(1,3),
"b"=c(2,4))
我找到了可以使用的线程Map(c, ls[[1]], ls[[2]])
。但是,如果写的ls
很长,那么写起来很麻烦。有捷径吗?
一种选择是 tidyverse
library(purrr)
library(dplyr)
transpose(ls) %>%
map(unlist)
或Map
与do.call
do.call(Map, c(f=c, ls))
#$a
#[1] 1 3
#$b
#[1] 2 4
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句