combine list elements based on element names

后端 未结 2 1650
说谎
说谎 2021-01-18 04:32

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 g

相关标签:
2条回答
  • 2021-01-18 04:51
    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
    
    0 讨论(0)
  • You can achieve the same result using map function from purrr

    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.

    0 讨论(0)
提交回复
热议问题