Jason Rupert
2009-Jun-09 12:48 UTC
[R] R command to join data.frames rows with identical keys?
I've got two data.frames and, when certain "keys" match, I would like to add the column values from one data frame to the other data.frame. Below I list the two data.frames, i.e. neighborhoodInfo_df, and schoolZone_df. Based on the "address" key I would like to add the "schoolZone" key to the neighborhoodInfo_df data.frame. By any chance is there an R command to accomplish this in one or two steps. I think I could do this in a "for" loop or something, but think there is might be another way in R to accomplish it smarter. Thanks for any info. neighborhoodInfo1_df<-data.frame(address<-c(101),squareFootage<-c(2000),lotsize<-c(0.75)) neighborhoodInfo2_df<-data.frame(address<-c(108),squareFootage<-c(3000), lotsize<-c(1.25)) neighborhoodInfo_df<-rbind(neighborhoodInfo1_df, neighborhoodInfo2_df) schoolZone1_df<-data.frame(address<-c(101), schoolZone<-c("Sherman")) schoolZone2_df<-data.frame(address<-c(108), schoolZone<-c("Baker")) schoolZone_df<-rbind(schoolZone1_df, schoolZone2_df)
Titus von der Malsburg
2009-Jun-09 12:57 UTC
[R] R command to join data.frames rows with identical keys?
Have a look at the merge function. "Merge two data frames by common columns or row names, or do other versions of database _join_ operations." Titus On Tue, Jun 09, 2009 at 05:48:05AM -0700, Jason Rupert wrote:> > I've got two data.frames and, when certain "keys" match, I would like to add the column values from one data frame to the other data.frame. > > Below I list the two data.frames, i.e. neighborhoodInfo_df, and schoolZone_df. Based on the "address" key I would like to add the "schoolZone" key to the neighborhoodInfo_df data.frame. > > By any chance is there an R command to accomplish this in one or two steps. I think I could do this in a "for" loop or something, but think there is might be another way in R to accomplish it smarter. > > Thanks for any info. > > > neighborhoodInfo1_df<-data.frame(address<-c(101),squareFootage<-c(2000),lotsize<-c(0.75)) > > neighborhoodInfo2_df<-data.frame(address<-c(108),squareFootage<-c(3000), lotsize<-c(1.25)) > > neighborhoodInfo_df<-rbind(neighborhoodInfo1_df, neighborhoodInfo2_df) > > > schoolZone1_df<-data.frame(address<-c(101), schoolZone<-c("Sherman")) > > schoolZone2_df<-data.frame(address<-c(108), schoolZone<-c("Baker")) > > schoolZone_df<-rbind(schoolZone1_df, schoolZone2_df) > > ______________________________________________ > 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.
Titus von der Malsburg
2009-Jun-09 13:06 UTC
[R] R command to join data.frames rows with identical keys?
An example:> schoolZone1_df <- data.frame(address=101, schoolZone="Sherman") > schoolZone2_df <- data.frame(address=108, schoolZone="Baker") > schoolZone_df <- rbind(schoolZone1_df, schoolZone2_df) > schoolZone_dfaddress schoolZone 1 101 Sherman 2 108 Baker> neighborhoodInfo1_df <- data.frame(address=101, squareFootage=2000, lotsize=0.75) > neighborhoodInfo2_df <- data.frame(address=108, squareFootage=3000, lotsize=1.25) > neighborhoodInfo_df <- rbind(neighborhoodInfo1_df, neighborhoodInfo2_df) > neighborhoodInfo_dfaddress squareFootage lotsize 1 101 2000 0.75 2 108 3000 1.25> merge(schoolZone_df, neighborhoodInfo_df, by="address")address schoolZone squareFootage lotsize 1 101 Sherman 2000 0.75 2 108 Baker 3000 1.25 Titus