Hi dear three helpers,
Thanks a lot! Your solutions worked great. Again I learned a lot.
Tagmarie
Am 25.10.2012 18:36, schrieb Felipe Carrillo:
> Another option using plyr,
> library(plyr)
> myframe <- data.frame (ID=c("Ernie", "Ernie", "Ernie", "Bert", "Bert",
> "Bert"), Timestamp=c("24.09.2012 09:00", "24.09.2012 10:00", "24.09.2012
> 11:00"), Hunger=c(1,1,1,2,2,1) )
> myframe
> myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp),
> "%d.%m.%Y %H:%M"), tz="GMT")
> myframestime
> myframe2 <- cbind (myframe,myframestime)
> myframe2
> ddply(myframe2,.(ID),summarise,Last2=mean(tail(Hunger,2)))
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
> http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>
> *From:* Tagmarie <[email protected]>
> *To:* [email protected]
> *Sent:* Thursday, October 25, 2012 7:35 AM
> *Subject:* [R] mean of a value of the last 2 hours
>
> Hello,
> I have a data frame somewhat like that:
>
> myframe <- data.frame (ID=c("Ernie", "Ernie", "Ernie", "Bert", "Bert",
> "Bert"), Timestamp=c("24.09.2012 09:00", "24.09.2012 10:00",
> "24.09.2012
> 11:00"), Hunger=c(1,1,1,2,2,1) )
> myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp),
> "%d.%m.%Y %H:%M"), tz="GMT")
> myframe2 <- cbind (myframe,myframestime)
> myframe2$Timestamp <- NULL
> myframe2
>
> I want to add an additional column at the right and get in each
> row a value
> which shows the mean of "hunger" of the last two hours.
>
> Does anyone know how that works? That would be very helpful.
>
>
>
> --
> View this message in context:
>
> http://r.789695.n4.nabble.com/mean-of-a-value-of-the-last-2-hours-tp4647415.html
> Sent from the R help mailing list archive at Nabble.com
> <http://nabble.com/>.
>
> ______________________________________________
> [email protected] <mailto:[email protected]> 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.
>
>
--
View this message in context:
http://r.789695.n4.nabble.com/Re-mean-of-a-value-of-the-last-2-hours-using-plyr-Thank-you-tp4647520.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.