Hello, Once again I feel so dumb. Can anyone help me? I have a data frame somewhat like that: myframe <- data.frame (ID=c("Ernie", "Ernie", "Bert", "Bert"), Timestamp=c("24.09.2012 09:00", "25.09.2012 09:00", "24.09.2012 10:00", "25.09.2012 10:00"), Hunger=c("1","1","2","2") ) myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") myframe2 <- cbind (myframe,myframestime) myframe2$Timestamp <- NULL Now I have a second dataframe with environmental variables. Something like this: environ <- data.frame (ID=c("Ernie", "Ernie", "Bert"), Timestamp=c("24.09.2012 09:00", "25.09.2012 09:00", "25.09.2012 10:00"), Temp=c("25","30","27"), Rain =c ("0.1", "5", "2")) envirotime <- as.POSIXct (strptime(as.character(environ$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") environ2 <- cbind (environ,envirotime) environ2$Timestamp <- NULL How do I merge or handle the two data frames at the respective lines so that I get the data (Temp, Rain) at the correct line with (Hunger)? -- View this message in context: http://r.789695.n4.nabble.com/add-a-data-frame-to-my-data-frame-tp4644122.html Sent from the R help mailing list archive at Nabble.com.
Is this what you want:> myframe2ID Hunger myframestime 1 Ernie 1 2012-09-24 09:00:00 2 Ernie 1 2012-09-25 09:00:00 3 Bert 2 2012-09-24 10:00:00 4 Bert 2 2012-09-25 10:00:00> environ2ID Temp Rain envirotime 1 Ernie 25 0.1 2012-09-24 09:00:00 2 Ernie 30 5 2012-09-25 09:00:00 3 Bert 27 2 2012-09-25 10:00:00> merge(myframe2, environ2, by.x = c("ID", "myframestime"), by.y = c("ID", "envirotime"))ID myframestime Hunger Temp Rain 1 Bert 2012-09-25 10:00:00 2 27 2 2 Ernie 2012-09-24 09:00:00 1 25 0.1 3 Ernie 2012-09-25 09:00:00 1 30 5 On Tue, Sep 25, 2012 at 10:44 AM, Tagmarie <Ramgad82 at gmx.net> wrote:> Hello, > Once again I feel so dumb. Can anyone help me? > I have a data frame somewhat like that: > > myframe <- data.frame (ID=c("Ernie", "Ernie", "Bert", "Bert"), > Timestamp=c("24.09.2012 09:00", "25.09.2012 09:00", "24.09.2012 10:00", > "25.09.2012 10:00"), Hunger=c("1","1","2","2") ) > myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), > "%d.%m.%Y %H:%M"), tz="GMT") > myframe2 <- cbind (myframe,myframestime) > myframe2$Timestamp <- NULL > > Now I have a second dataframe with environmental variables. Something like > this: > > environ <- data.frame (ID=c("Ernie", "Ernie", "Bert"), > Timestamp=c("24.09.2012 09:00", "25.09.2012 09:00", "25.09.2012 10:00"), > Temp=c("25","30","27"), Rain =c ("0.1", "5", "2")) > envirotime <- as.POSIXct (strptime(as.character(environ$Timestamp), > "%d.%m.%Y %H:%M"), tz="GMT") > environ2 <- cbind (environ,envirotime) > environ2$Timestamp <- NULL > > How do I merge or handle the two data frames at the respective lines so that > I get the data (Temp, Rain) at the correct line with (Hunger)? > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/add-a-data-frame-to-my-data-frame-tp4644122.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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
How about:> merge(myframe2, environ2, all.x=TRUE, by.x=c("ID", "myframestime"), by.y=c("ID", "envirotime"))ID myframestime Hunger Temp Rain 1 Bert 2012-09-24 10:00:00 2 <NA> <NA> 2 Bert 2012-09-25 10:00:00 2 27 2 3 Ernie 2012-09-24 09:00:00 1 25 0.1 4 Ernie 2012-09-25 09:00:00 1 30 5 No need to feel dumb, but learning to use rseek.org to search for R topics might help you solve some of your own problems. In this case searching for merge data frames gives you all sorts of useful information. And thank you for providing a simple reproducible example! Sarah On Tue, Sep 25, 2012 at 10:44 AM, Tagmarie <Ramgad82 at gmx.net> wrote:> Hello, > Once again I feel so dumb. Can anyone help me? > I have a data frame somewhat like that: > > myframe <- data.frame (ID=c("Ernie", "Ernie", "Bert", "Bert"), > Timestamp=c("24.09.2012 09:00", "25.09.2012 09:00", "24.09.2012 10:00", > "25.09.2012 10:00"), Hunger=c("1","1","2","2") ) > myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), > "%d.%m.%Y %H:%M"), tz="GMT") > myframe2 <- cbind (myframe,myframestime) > myframe2$Timestamp <- NULL > > Now I have a second dataframe with environmental variables. Something like > this: > > environ <- data.frame (ID=c("Ernie", "Ernie", "Bert"), > Timestamp=c("24.09.2012 09:00", "25.09.2012 09:00", "25.09.2012 10:00"), > Temp=c("25","30","27"), Rain =c ("0.1", "5", "2")) > envirotime <- as.POSIXct (strptime(as.character(environ$Timestamp), > "%d.%m.%Y %H:%M"), tz="GMT") > environ2 <- cbind (environ,envirotime) > environ2$Timestamp <- NULL > > How do I merge or handle the two data frames at the respective lines so that > I get the data (Temp, Rain) at the correct line with (Hunger)? > >-- Sarah Goslee http://www.functionaldiversity.org
HI, I guess this is what you are looking for: merge(myframe2,environ2, by="ID") #???? ID Hunger??????? myframestime Temp Rain????????? envirotime #1? Bert????? 2 2012-09-24 10:00:00?? 27??? 2 2012-09-25 10:00:00 #2? Bert????? 2 2012-09-25 10:00:00?? 27??? 2 2012-09-25 10:00:00 #3 Ernie????? 1 2012-09-24 09:00:00?? 25? 0.1 2012-09-24 09:00:00 #4 Ernie????? 1 2012-09-24 09:00:00?? 30??? 5 2012-09-25 09:00:00 #5 Ernie????? 1 2012-09-25 09:00:00?? 25? 0.1 2012-09-24 09:00:00 #6 Ernie????? 1 2012-09-25 09:00:00?? 30??? 5 2012-09-25 09:00:00 A.K. ----- Original Message ----- From: Tagmarie <Ramgad82 at gmx.net> To: r-help at r-project.org Cc: Sent: Tuesday, September 25, 2012 10:44 AM Subject: [R] add a data frame to my data frame Hello, Once again I feel so dumb. Can anyone help me? I have a data frame somewhat like that: myframe <- data.frame (ID=c("Ernie", "Ernie", "Bert", "Bert"), Timestamp=c("24.09.2012 09:00", "25.09.2012 09:00", "24.09.2012 10:00", "25.09.2012 10:00"), Hunger=c("1","1","2","2") ) myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") myframe2 <- cbind (myframe,myframestime) myframe2$Timestamp <- NULL Now I have a second dataframe with environmental variables. Something like this: environ <- data.frame (ID=c("Ernie", "Ernie", "Bert"), Timestamp=c("24.09.2012 09:00", "25.09.2012 09:00", "25.09.2012 10:00"), Temp=c("25","30","27"), Rain =c ("0.1", "5", "2")) envirotime <- as.POSIXct (strptime(as.character(environ$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") environ2 <- cbind (environ,envirotime) environ2$Timestamp <- NULL How do I merge or handle the two data frames at the respective lines so that I get the data (Temp, Rain) at the correct line with (Hunger)? -- View this message in context: http://r.789695.n4.nabble.com/add-a-data-frame-to-my-data-frame-tp4644122.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.