Mary Kindall
2011-Jun-14 15:40 UTC
[R] [Resolved] combine the data frames into comma separated list.
Hi Thanks Gabor for your suggestion. I am posting the code that worked for me. dataframe1 = data.frame(cbind(Src = c(1,1,1,2,3), Target1 c('aaa','bbb','ccc','aaa','ddd'))); #must be data frame dataframe2 = data.frame(cbind(Src = c(2,3,4,4,4), Target2 c('aaaa','dddd','bbbb','eeee','ffff'))); dataframe3 = data.frame(cbind(Src = c(1,3,5,6,6), Target3 c('xx','yy','zz','tt','uu'))); dataframe4 = data.frame(cbind(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))) Cheers !!! - M On Tue, Jun 14, 2011 at 11:27 AM, Gabor Grothendieck < ggrothendieck@gmail.com> wrote:> On Tue, Jun 14, 2011 at 11:21 AM, Mary Kindall <mary.kindall@gmail.com> > wrote: > > I resolved it. There was a problem in type casting at some point in my > > program. > > Thanks again. > > - > > Please post a corrected version of L for benefit of others who were > following this. > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com >-- ------------- Mary Kindall Yorktown Heights, NY USA [[alternative HTML version deleted]]
Gabor Grothendieck
2011-Jun-14 15:52 UTC
[R] [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:> Hi > Thanks Gabor for your suggestion. I am posting the code that worked for me. > > > dataframe1 = data.frame(cbind(Src = c(1,1,1,2,3), Target1 > c('aaa','bbb','ccc','aaa','ddd')));? #must be data frame > dataframe2 = data.frame(cbind(Src = c(2,3,4,4,4), Target2 > c('aaaa','dddd','bbbb','eeee','ffff'))); > dataframe3 = data.frame(cbind(Src = c(1,3,5,6,6), Target3 > c('xx','yy','zz','tt','uu'))); > dataframe4 = data.frame(cbind(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))) >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'). -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com