[R] using ddply but preserving some of the outside data
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Aug 6 01:42:04 CEST 2009
Its not the evenly spaced that makes it work -- its that you want the
same number of observations to contribute to each mean even if they
are not equally spaced.
In your case since you want different numbers of observations to
contribute to each mean you will need a different approach:
library(zoo)
a.wide <- reshape(a.df, dir = "wide", timevar = "dates", idvar = "sites")
a2 <- t(a.wide[,-1])
z <- zoo(a2, as.numeric(sub(".*[.]", "", rownames(a2))))
mns <- t(sapply(time(z), function(tt) colMeans(a2[abs(tt - time(z)) < 20,])))
colnames(mns) <- a.wide$sites
z.out <- zoo(mns, time(z))
z.out
There are three vignettes that come with zoo that you can read.
On Wed, Aug 5, 2009 at 6:49 PM, jebyrnes<byrnes at msi.ucsb.edu> wrote:
>
> OK, I see how this would work for a rolling mean with continuous
> observations. However, my actual problem is a bit more complex. I have a
> number of observations, not necessarily evenly spaced. So, the data I'm
> working with looks somewhat more like what this would produce
>
> set.seed(2003)
> sites<-c("a", "b", "c")
> dates<-round(runif(20,0,50))
>
> a.df<-expand.grid(sites=sites, dates=dates)
> a.df$value<-runif(30,0,100)
> a.df<-as.data.frame(a.df)
>
> And, rather than getting a rolling daily average, let's say I want an
> average from any sample points from the previous 20 days.
>
> #now, I want to get the average of the
> mean2<-function(df, date){
> sub.df<-subset(df, df$dates-date<20 &
> df$dates-date>-20 )
> return(mean(df$value))
> }
>
> Would the rollmean approach still work here?
>
>
> Gabor Grothendieck wrote:
>>
>> In particular, try this:
>>
>>> library(zoo)
>>> a.wide <- reshape(a.df, dir = "wide", timevar = "dates", idvar = "sites")
>>> rollmean(as.zoo(t(a.wide[,-1])), 2)
>>
>> 1 56.855685 58.62981 95.14842
>> 2 58.049821 58.81659 78.70020
>> 3 11.199634 89.91179 76.22853
>> 4 1.152741 43.63333 93.03040
>>
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/using-ddply-but-preserving-some-of-the-outside-data-tp24834295p24837587.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.
>
More information about the R-help
mailing list