Skip to content
Prev 262675 / 398502 Next

[Resolved] combine the data frames into comma separated list.

On Tue, Jun 14, 2011 at 11:40 AM, Mary Kindall <mary.kindall at gmail.com> wrote:
Note that
- cbind is not needed here
- R statements need not be terminated with semicolons
so the code can be slightly reduced as shown below.

dataframe1 <- data.frame(Src = c(1,1,1,2,3), Target1 =
c('aaa','bbb','ccc','aaa','ddd'))
dataframe2 <- data.frame(Src = c(2,3,4,4,4), Target2 =
c('aaaa','dddd','bbbb','eeee','ffff'))
dataframe3 <- data.frame(Src = c(1,3,5,6,6), Target3 =
c('xx','yy','zz','tt','uu'))
dataframe4 <- data.frame(Src = c(3,5,'y','z','z'), Target4 =
c('xx','yy','zz','tt','uu'))

L <- list(dataframe1, dataframe2, dataframe3, dataframe4)
merge.all <- function(...) merge(..., all = TRUE)
Reduce(merge.all, lapply(L, function(x) aggregate(x[2], x[1], toString)))

Also note that c(3, 5, 'y', 'z', 'z') will be converted to c('3', '5',
'y', 'z', 'z').