[R] combine the data frames into comma separated list.
Dr. D. P. Kreil (Boku)
David.Kreil at boku.ac.at
Tue Jun 14 02:01:08 CEST 2011
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:
> How??
> I dont think there is any parameter that does this job.
>
> I came up with ddply function in plyr package but having tens of dataframe
> and doing it in a for loop may not be a good idea.
>
> ddply(test, ~ Src , colwise(paste, .(Target1)), collapse ="," );
>
> Can you please write how it can be done by write.csv.
>
> Or is there any efficient method that can do this for me.
>
> dataframe1 = data.frame(cbind(Src = c(1,1,1,2,3), Target1 =
> c('aaa','bbb','ccc','aaa','ddd')));
> 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')));
> test = merge(dataframe3, merge(dataframe1,dataframe2, by = 'Src', all=TRUE,
> incomparables=''), by = 'Src', all=TRUE, incomparables='')
> ddply(test, ~ Src , colwise(paste, .(Target1)), collapse ="," );
>
>
> Thanks
>
>
>
>
>
>
>
> On Mon, Jun 13, 2011 at 7:14 PM, Dr. D. P. Kreil (Boku)
> <David.Kreil at boku.ac.at> wrote:
>>
>> ?write.csv
>>
>> Cheers,
>> David.
>>
>>
>> On 14 June 2011 01:07, Mary Kindall <mary.kindall at gmail.com> wrote:
>> > Thanks for reply.
>> > The following code is working but only patially. How to get the
>> > condensed
>> > values separated by comma.
>> >
>> > dataframe1 = data.frame(cbind(Src = c(1,1,1,2,3), Target1 =
>> > c('aaa','bbb','ccc','aaa','ddd')));
>> > 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')));
>> > merge(dataframe3, merge(dataframe1,dataframe2, by = 'Src', all=TRUE), by
>> > =
>> > 'Src', all=TRUE)
>> >
>> >
>> > 1> merge(dataframe3, merge(dataframe1,dataframe2, by = 'Src', all=TRUE),
>> > by
>> > = 'Src', all=TRUE)
>> > Src Target3 Target1 Target2
>> > 1 1 xx aaa <NA>
>> > 2 1 xx bbb <NA>
>> > 3 1 xx ccc <NA>
>> > 4 3 yy ddd dddd
>> > 5 5 zz <NA> <NA>
>> > 6 6 tt <NA> <NA>
>> > 7 6 uu <NA> <NA>
>> > 8 2 <NA> aaa aaaa
>> > 9 4 <NA> <NA> bbbb
>> > 10 4 <NA> <NA> eeee
>> > 11 4 <NA> <NA> ffff
>> >
>> > Thanks
>> >
>> > --
>> > M
>> >
>> >
>> > On Mon, Jun 13, 2011 at 6:35 PM, Dr. D. P. Kreil (Boku)
>> > <David.Kreil at boku.ac.at> wrote:
>> >>
>> >> Hi, try
>> >>
>> >> ?merge
>> >>
>> >> Best,
>> >> David.
>> >>
>> >>
>> >> On 13 June 2011 23:48, Mary Kindall <mary.kindall at gmail.com> wrote:
>> >> > Hi R users,
>> >> > I am new to R and am trying to merge data frames in the following
>> >> > way.
>> >> > Suppose I have n data frames each with two fields. Field 1 is common
>> >> > among
>> >> > data frames but may have different entries. Field 2 is different.
>> >> >
>> >> >
>> >> > Data frame 1:
>> >> >
>> >> > Src Target1
>> >> > 1 aaa
>> >> > 1 bbb
>> >> > 1 ccc
>> >> > 2 aaa
>> >> > 3 ddd
>> >> >
>> >> >
>> >> > Data frame 2:
>> >> >
>> >> > Src Target2
>> >> > 2 aaaa
>> >> > 3 dddd
>> >> > 4 bbbb
>> >> > 4 eeee
>> >> > 4 ffff
>> >> >
>> >> >
>> >> > Data frame 3:
>> >> >
>> >> > Src Target3
>> >> > 1 xx
>> >> > 3 yy
>> >> > 5 zz
>> >> > 6 tt
>> >> > 6 uu
>> >> >
>> >> > And so on...
>> >> >
>> >> > I want to convert this into a data frame something similar to:
>> >> > Src Target1 target2
>> >> > target3
>> >> > 1 aaa,bbb,ccc -
>> >> > xx
>> >> >
>> >> > 2 aaa aaaa
>> >> > -
>> >> > 3 ddd dddd
>> >> > yy
>> >> > 4 - bbbb,eeee,ffff
>> >> > -
>> >> >
>> >> > 5 -
>> >> > - zz
>> >> > 6 -
>> >> > - tt,uu
>> >> >
>> >> >
>> >> > Basically I am trying to make a consolidated table.
>> >> >
>> >> > Help appreciated.
>> >> > Thanks
>> >> > M
>> >> >
>> >> >
>> >> > -------------
>> >> > Mary Kindall
>> >> > Yorktown Heights
>> >> > USA
>> >> >
>> >> > [[alternative HTML version deleted]]
>> >> >
>> >> > ______________________________________________
>> >> > R-help at r-project.org mailing list
>> >> > https://stat.ethz.ch/mailman/listinfo/r-help
>> >> > PLEASE do read the posting guide
>> >> > http://www.R-project.org/posting-guide.html
>> >> > and provide commented, minimal, self-contained, reproducible code.
>> >> >
>> >
>> >
>> >
>> > --
>> > -------------
>> > Mary Kindall
>> > Yorktown Heights, NY
>> > USA
>> >
>> >
>
>
>
> --
> -------------
> Mary Kindall
> Yorktown Heights, NY
> USA
>
>
More information about the R-help
mailing list