Combine/merge lists by elements names (list in list)

前端 未结 5 930
渐次进展
渐次进展 2021-01-11 20:42

I have two lists, whose elements have partially overlapping names, which I need to merge/combine together into a single list, element by element:

My question is rela

5条回答
  •  不要未来只要你来
    2021-01-11 20:55

    Here you go in 3 lines:

    out <- l.1
    mnames <- intersect(names(l.1),names(l.2))
    out[mnames] <- Map(function(a,b) Map(c,a,b),l.1[mnames],l.2[mnames])
    
    #$a
    #$a[[1]]
    #[1] 10 20
    #$a[[2]]
    #[1] 1 0
    #
    #$b
    #$b[[1]]
    #[1] 10 20 30
    #$b[[2]]
    #[1] 1 2 3
    #
    #$c
    #$c[[1]]
    #[1]  9 12 13
    #$c[[2]]
    #NULL
    

提交回复
热议问题