2

I have two list like that

A<-list(a=1,b=2,c=3)
B<-list(a=4,e=5,f=6)

and I want to merge them. Anyway if I do

 c(A,B)

what I get is

$a
[1] 1

$b
[1] 2

$c
[1] 3

$a
[1] 4

$e
[1] 5

$f
[1] 6

why what I actually want is

$a
[1] 1 4

$b
[1] 2

$c
[1] 3

$e
[1] 5

$f
[1] 6

Can someone help me?

Many thanks

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
user3036416
  • 1,205
  • 2
  • 15
  • 28

2 Answers2

1
sapply(unique(c(names(A), names(B))), function(x) list(c(A[[x]], B[[x]])))
sedsiv
  • 531
  • 1
  • 3
  • 15
1

I think both the comment and the answer above are correct, but -for fun- would still like to propose:

tapply(unlist(c(A,B),use.names=F),names(c(A,B)),c)
texb
  • 547
  • 2
  • 13