mean of a value of the last 2 hours

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

mean of a value of the last 2 hours

Tagmarie
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.
Reply | Threaded
Open this post in threaded view
|

Re: mean of a value of the last 2 hours

arun kirshna
Hi,
May be this helps:
 new1<-with(myframe2,aggregate(cbind(Hunger,myframestime),by=list(ID=ID), function(x) mean(tail(x,2))))[,1:2]
 merge(myframe2,new1,by="ID",all=TRUE)
#     ID Hunger.x        myframestime Hunger.y
#1  Bert        2 2012-09-24 09:00:00      1.5
#2  Bert        2 2012-09-24 10:00:00      1.5
#3  Bert        1 2012-09-24 11:00:00      1.5
#4 Ernie        1 2012-09-24 09:00:00      1.0
#5 Ernie        1 2012-09-24 10:00:00      1.0
#6 Ernie        1 2012-09-24 11:00:00      1.0
A.K.




----- Original Message -----
From: Tagmarie <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, October 25, 2012 10: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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: mean of a value of the last 2 hours

connerphd
This post has NOT been accepted by the mailing list yet.
In reply to this post by Tagmarie
I believe that a Rollong function (zoo) might cure what ails you. http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/zoo/html/rollapply.html
Reply | Threaded
Open this post in threaded view
|

Re: mean of a value of the last 2 hours

connerphd
This post has NOT been accepted by the mailing list yet.
Rolling, rather..... Darn auto correct
Reply | Threaded
Open this post in threaded view
|

Re: mean of a value of the last 2 hours

Rui Barradas
In reply to this post by arun kirshna
Hello,

Or


ct <- cut(myframe2$myframestime, breaks = "2 hour")
ave(myframe2$Hunger, ct)

And assign the output of 'ave' to a new column.

Hope this helps,

Rui Barradas
Em 25-10-2012 15:50, arun escreveu:

> Hi,
> May be this helps:
>   new1<-with(myframe2,aggregate(cbind(Hunger,myframestime),by=list(ID=ID), function(x) mean(tail(x,2))))[,1:2]
>   merge(myframe2,new1,by="ID",all=TRUE)
> #     ID Hunger.x        myframestime Hunger.y
> #1  Bert        2 2012-09-24 09:00:00      1.5
> #2  Bert        2 2012-09-24 10:00:00      1.5
> #3  Bert        1 2012-09-24 11:00:00      1.5
> #4 Ernie        1 2012-09-24 09:00:00      1.0
> #5 Ernie        1 2012-09-24 10:00:00      1.0
> #6 Ernie        1 2012-09-24 11:00:00      1.0
> A.K.
>
>
>
>
> ----- Original Message -----
> From: Tagmarie <[hidden email]>
> To: [hidden email]
> Cc:
> Sent: Thursday, October 25, 2012 10: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.
>
> ______________________________________________
> [hidden email] 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.
>
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: mean of a value of the last 2 hours

Felipe Carrillo
In reply to this post by Tagmarie
Or using ddply from 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 <[hidden email]>

>To: [hidden email]
>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.
>
>______________________________________________
>[hidden email] 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.
>
>
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: mean of a value of the last 2 hours

S Ellison-2
In reply to this post by Rui Barradas
> > 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.

Isn't that just a moving average?

if so, see http://stackoverflow.com/questions/743812/calculating-moving-average-in-r, which mentions zoo for RollingMeans, TTR for MovingAverages, forecast for ma and a possible solution using filter. One of those might help.

S Ellison

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
[hidden email] 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.