[R] add a data frame to my data frame

Sarah Goslee sarah.goslee at gmail.com
Tue Sep 25 17:37:00 CEST 2012


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




More information about the R-help mailing list