jeff6868
2012-Jul-18 08:21 UTC
[R] duplicate data between two data frames according to row names
Hi everybody. I'll first explain my problem and what I'm trying to do. Admit this example: I'm working on 5 different weather stations. I have first in one file 3 of these 5 weather stations, containing their data. Here's an example of this file: DF1 <- data.frame(station=c("ST001","ST004","ST005"),data=c(5,2,8)) And my two other stations in this other data.frame: DF2 <- data.frame(station=c("ST002","ST003"),data=c(3,7)) I would like to add geographical coordinates of these weather stations inside these two data.frames, according to the number of the weather station. All of my geographical coordinates for each of the 5 weather stations are inside another data frame: DF3 <- data.frame(station=c("ST001","ST002","ST003","ST004","ST005"),lat=c(40,41,42,43,44),lon=c(1,2,3,4,5)) My question is: how can I put automatically these geographical coordinates inside my first 2 data frames, according to the number of the weather station? For this example, the first two data frames DF1 and DF2 should become: DF1 <- data.frame(station=c("ST001","ST004","ST005"),lat=c(40,43,44),lon=c(1,4,5),data=c(5,2,8)) and DF2 <- data.frame(station=c("ST002","ST003"),lat=c(41,42),lon=c(2,3),data=c(3,7)) I need to automatize this method because my real dataset contains 70 weather stations, and each file contains other (or same sometimes) stations , but each station can be found in the list of the coordinates file (DF3). Is there any way or any function able to do this kind of thing? Thank you very much! -- View this message in context: http://r.789695.n4.nabble.com/duplicate-data-between-two-data-frames-according-to-row-names-tp4636845.html Sent from the R help mailing list archive at Nabble.com.
Eik Vettorazzi
2012-Jul-18 09:19 UTC
[R] duplicate data between two data frames according to row names
Hi Jeff, looks like a job for ?rbind and ?merge merge(rbind(DF1,DF2),DF3) hth Am 18.07.2012 10:21, schrieb jeff6868:> Hi everybody. > > I'll first explain my problem and what I'm trying to do. > Admit this example: > I'm working on 5 different weather stations. > I have first in one file 3 of these 5 weather stations, containing their > data. Here's an example of this file: > > DF1 <- data.frame(station=c("ST001","ST004","ST005"),data=c(5,2,8)) > > And my two other stations in this other data.frame: > > DF2 <- data.frame(station=c("ST002","ST003"),data=c(3,7)) > > I would like to add geographical coordinates of these weather stations > inside these two data.frames, according to the number of the weather > station. > > All of my geographical coordinates for each of the 5 weather stations are > inside another data frame: > > DF3 <- > data.frame(station=c("ST001","ST002","ST003","ST004","ST005"),lat=c(40,41,42,43,44),lon=c(1,2,3,4,5)) > > My question is: how can I put automatically these geographical coordinates > inside my first 2 data frames, according to the number of the weather > station? > > For this example, the first two data frames DF1 and DF2 should become: > > DF1 <- > data.frame(station=c("ST001","ST004","ST005"),lat=c(40,43,44),lon=c(1,4,5),data=c(5,2,8)) > and > DF2 <- > data.frame(station=c("ST002","ST003"),lat=c(41,42),lon=c(2,3),data=c(3,7)) > > I need to automatize this method because my real dataset contains 70 weather > stations, and each file contains other (or same sometimes) stations , but > each station can be found in the list of the coordinates file (DF3). > > Is there any way or any function able to do this kind of thing? > > Thank you very much! > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/duplicate-data-between-two-data-frames-according-to-row-names-tp4636845.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus
arun
2012-Jul-18 12:29 UTC
[R] duplicate data between two data frames according to row names
Hi, You could use "merge", "join" etc. merge(DF3,DF2) ?# station lat lon data #1?? ST002? 41?? 2??? 3 #2?? ST003? 42?? 3??? 7 library(plyr) ?join(DF3,DF2,type="inner") Joining by: station #? station lat lon data #1?? ST002? 41?? 2??? 3 #2?? ST003? 42?? 3??? 7 #or join(DF3,DF2,type="right") Hope this helps A.K. ----- Original Message ----- From: jeff6868 <geoffrey_klein at etu.u-bourgogne.fr> To: r-help at r-project.org Cc: Sent: Wednesday, July 18, 2012 4:21 AM Subject: [R] duplicate data between two data frames according to row names Hi everybody. I'll first explain my problem and what I'm trying to do. Admit this example: I'm working on 5 different weather stations. I have first in one file 3 of these 5 weather stations, containing their data. Here's an example of this file: DF1 <- data.frame(station=c("ST001","ST004","ST005"),data=c(5,2,8)) And my two other stations in this other data.frame: DF2 <- data.frame(station=c("ST002","ST003"),data=c(3,7)) I would like to add geographical coordinates of these weather stations inside these two data.frames, according to the number of the weather station. All of my geographical coordinates for each of the 5 weather stations are inside another data frame: DF3 <- data.frame(station=c("ST001","ST002","ST003","ST004","ST005"),lat=c(40,41,42,43,44),lon=c(1,2,3,4,5)) My question is: how can I put automatically these geographical coordinates inside my first 2 data frames, according to the number of the weather station? For this example, the first two data frames DF1 and DF2 should become: DF1 <- data.frame(station=c("ST001","ST004","ST005"),lat=c(40,43,44),lon=c(1,4,5),data=c(5,2,8)) and DF2 <- data.frame(station=c("ST002","ST003"),lat=c(41,42),lon=c(2,3),data=c(3,7)) I need to automatize this method because my real dataset contains 70 weather stations, and each file contains other (or same sometimes) stations , but each station can be found in the list of the coordinates file (DF3). Is there any way or any function able to do this kind of thing? Thank you very much! -- View this message in context: http://r.789695.n4.nabble.com/duplicate-data-between-two-data-frames-according-to-row-names-tp4636845.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.