[R] merging a list and data frame

Mark Lyman mark.lyman at atk.com
Wed Oct 1 15:55:30 CEST 2008


eric lee <ericlee100 <at> gmail.com> writes:

> 
> Hi,
> 
> I'd like to merge the following list and data frame by matching the first
> column in the data frame to the first number in each object of the list.
> I'd also like the merged object to be a list.  Any suggestions?  Thanks.
> 
> eric
> 
> a <- list(c(1,11), c(2,12), c(3,13), c(4,14), c(5,16))
> 
> b <- data.frame(1:5, 51:55)
>

Here is one idea:

> a <- list(c(1,11), c(2,12), c(3,13), c(4,14), c(5,16))
> b <- data.frame(1:5, 51:55)
> tmp <- merge(do.call("rbind", a), b, by=1)
> split(as.matrix(tmp), row(tmp))
$`1`
[1]  1 11 51

$`2`
[1]  2 12 52

$`3`
[1]  3 13 53

$`4`
[1]  4 14 54

$`5`
[1]  5 16 55



More information about the R-help mailing list