简体   繁体   English

根据元素名称组合列表元素

[英]combine list elements based on element names

How to combine this list of vectors by elements names ? 如何通过元素名称组合这个向量列表?

L1 <- list(F01=c(1,2,3,4),F02=c(10,20,30),F01=c(5,6,7,8,9),F02=c(40,50))

So to get : 所以得到:

results <- list(F01=c(1,2,3,4,5,6,7,8),F02=c(10,20,30,40,50))

I tried to apply the following solution merge lists by elements names but I can't figure out how to adapt this to my situation. 我尝试按元素名称应用以下解决方案合并列表,但我无法弄清楚如何使其适应我的情况。

sapply(unique(names(L1)), function(x) unname(unlist(L1[names(L1)==x])), simplify=FALSE)
$F01
[1] 1 2 3 4 5 6 7 8 9

$F02
[1] 10 20 30 40 50

You can achieve the same result using map function from purrr 您可以使用purrr map函数获得相同的结果

map(unique(names(L1)), ~ flatten_dbl(L1[names(L1) == .x])) %>%
  set_names(unique(names(L1)))

The first line transforms the data by merging elements with matching names, while the last line renames new list accordingly. 第一行通过合并具有匹配名称的元素来转换数据,而最后一行相应地重命名新列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM