[R] using ddply but preserving some of the outside data
jebyrnes
byrnes at msi.ucsb.edu
Thu Aug 6 00:49:53 CEST 2009
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.
More information about the R-help
mailing list