Skip to content

merge list entries

3 messages · alejandro munoz, jim holtman

#
dear expeRts,

i would like to merge the data frame entries in a list. for example:
q2=data.frame(id=c("Alice", "Chuck"), grade=c(70, 93)),
              q3=data.frame(id=c("Bob", "Chuck"), grade=c(84, 40)))
id grade.1 grade.2 grade.3
1 Alice      90      70      NA
2   Bob      49      NA      84
3 Chuck      NA      93      40

my three attempts, and their error messages, follow:
Error in as.data.frame(y) : argument "y" is missing, with no default
Error in lapply(myl[[-1]], merge, y = myl[[1]], by = "id", all = TRUE) : 
	attempt to select more than one element
Error in do.call("merge", list(myl[[1]], myl[[-1]], by = "id", all = TRUE)) : 
	attempt to select more than one element

i can do the merge sequentially, e.g. 
m12 <- merge(myl[[1]], myl[[2]], ...)
m123 <- merge(m12, myl[[3]], ...)
but (a) in my actual example i have up to q7, and (b) this looks very
clumsy, even if i wrapped it inside a do loop.

i'd appreciate any help.

alejandro
#
Try:
+     cbind(myl[[x]], q=x)
+ }))
id grade.1 grade.2 grade.3
1  Alice      90      70      NA
2    Bob      49      NA      84
21 Chuck      NA      93      40
id grade q
1  Alice    90 1
2    Bob    49 1
11 Alice    70 2
21 Chuck    93 2
12   Bob    84 3
22 Chuck    40 3
On 8/23/05, alejandro munoz <guerinche at gmail.com> wrote: