[R] add a data frame to my data frame

jim holtman jholtman at gmail.com
Tue Sep 25 17:34:44 CEST 2012


Is this what you want:

> myframe2
     ID 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
> environ2
     ID 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.




More information about the R-help mailing list