Skip to content

combine the data frames into comma separated list.

10 messages · Dr. D. P. Kreil (Boku), Gabor Grothendieck, Mary Kindall

#
Hi, try

?merge

Best,
David.
On 13 June 2011 23:48, Mary Kindall <mary.kindall at gmail.com> wrote:
#
?write.csv

Cheers,
David.
On 14 June 2011 01:07, Mary Kindall <mary.kindall at gmail.com> wrote:
#
Ah, yes, the need to "condense" escaped me. I thought you wanted to
write out the final result separated by commas.

You will need to do the "condensing" separately. Unless someone else
has a more elegant idea, I guess something along the lines of

df2srcs<-unique(dataframe2$Src);
df2new<-data.frame(Src=df2srcs,Target2=sapply(df2srcs,function(s){paste(dataframe2$Target2[dataframe2$Src==s],collapse=',')}));

should do the trick.

Cheers,
David.
On 14 June 2011 01:34, Mary Kindall <mary.kindall at gmail.com> wrote:
#
On Mon, Jun 13, 2011 at 5:17 PM, Mary Kindall <mary.kindall at gmail.com> wrote:
Try this where DF1, DF2 and DF3 are the data frames:

L <- list(DF1, DF2, DF3)
merge.all <- function(...) merge(..., all = TRUE)
Reduce(merge.all, lapply(L, function(x) aggregate(x[2], x[1], toString)))

The last line gives this:

  Src       Target1          Target2 Target3
1   1 aaa, bbb, ccc             <NA>      xx
2   2           aaa             aaaa    <NA>
3   3           ddd             dddd      yy
4   4          <NA> bbbb, eeee, ffff    <NA>
5   5          <NA>             <NA>      zz
6   6          <NA>             <NA>  tt, uu
#
now that's beautiful! :-)

Many thanks,
David.