[R] [Resolved] combine the data frames into comma separated list.
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Jun 14 17:52:15 CEST 2011
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
More information about the R-help
mailing list