Hi: I am a new R user. I have the following question and would appreciate your input Data1 (data frame 1) p1,d1,d2 (p1 is text and d1 and d2 are numeric) xyz,10,25 Data2 (data frame 2) p1,d1,d2 xyz,11,15 Now I want to create a new data frame that looks like so below. The fields d1 and s2 are summed by the product key. Data3 p1,d1,d2 xyz,21 (sum of 10 from Data1 and 11 from Data2),40 (sum of 25 from Data1 and 15 from Data2) Any other examples of merge you may have will be appreciated. Thanks. Satish
Hi there, something like this? Data1<-read.table(stdin(), head=T, sep=",") p1,d1,d2 xyz,10,25 kmz,100,250 Data2<-read.table(stdin(), head=T, sep=",") p1,d1,d2 xyz,11,15 kmz,110,150 Data1 Data2 Data3<-data.frame(rbind(Data1,Data2)) Data3 Data3.sum<-aggregate(Data3[,c("d1","d2")], list(Data3$p1), sum) Data3.sum Best wishes miltinho astronauta brazil On Thu, Feb 26, 2009 at 2:52 PM, Vadlamani, Subrahmanyam {FLNA} < subrahmanyam.vadlamani@fritolay.com> wrote:> Hi: > I am a new R user. I have the following question and would appreciate your > input > > Data1 (data frame 1) > p1,d1,d2 (p1 is text and d1 and d2 are numeric) > xyz,10,25 > > Data2 (data frame 2) > p1,d1,d2 > xyz,11,15 > > Now I want to create a new data frame that looks like so below. The fields > d1 and s2 are summed by the product key. > Data3 > p1,d1,d2 > xyz,21 (sum of 10 from Data1 and 11 from Data2),40 (sum of 25 from Data1 > and 15 from Data2) > > Any other examples of merge you may have will be appreciated. Thanks. > Satish > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
on 02/26/2009 11:52 AM Vadlamani, Subrahmanyam {FLNA} wrote:> Hi: > I am a new R user. I have the following question and would appreciate your input > > Data1 (data frame 1) > p1,d1,d2 (p1 is text and d1 and d2 are numeric) > xyz,10,25 > > Data2 (data frame 2) > p1,d1,d2 > xyz,11,15 > > Now I want to create a new data frame that looks like so below. The fields d1 and s2 are summed by the product key. > Data3 > p1,d1,d2 > xyz,21 (sum of 10 from Data1 and 11 from Data2),40 (sum of 25 from Data1 and 15 from Data2) > > Any other examples of merge you may have will be appreciated. Thanks. > SatishGiven the nature of your data, having the same column structure with repeated keys, I would not use merge(), but rbind() the two data frames together and then use aggregate(): DF <- rbind(Data1, Data2)> DFp1 d1 d2 1 xyz 10 25 2 xyz 11 15> aggregate(DF[-1], list(p1 = DF$p1), sum)p1 d1 d2 1 xyz 21 40 See ?rbind and ?aggregate If you search the list archives: RSiteSearch("merge") you will yield hundreds of posts showing the use of that particular function. HTH, Marc Schwartz