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 On Wed, Aug 5, 2009 at 3:05 PM, David Winsemius<dwinsem...@comcast.net> wrote: > > > library(zoo) > ?rollmean > > On Aug 5, 2009, at 3:00 PM, Jarrett Byrnes wrote: > >> I have a bit of a quandy. I'm working with a data set for which I have >> sampled sites at a variety of dates. I want to use this data, and get a >> running average of the sampled values for the current and previous date. >> >> I originally thought something like ddply would be ideal for this, >> however, I cannot break up my data by date, and then apply a function that >> requires information about the previous dates. >> >> I had thought to use a for loop and merge, but that doesn't quite seem to >> be working. >> >> So, my questions are twofold >> >> 1) Is there a way to use something like the plyr library to do this >> efficiently >> 1a) Indeed, is there a way to use ddply or its ilk to have a >> function that returns a vector of values, and then assign the variables you >> are sorting by to the whole vector? Or maybe making each value it's own >> column in the new data frame, and then using reshape is the answer. Hrm. >> Seems clunky. >> >> 2) Or, can a for loop around a plyr-kind of statement do the trick (and if >> so, pointers on why the below code won't work) (also, it, too, seems >> clunkier than I would like) >> >> >> sites<-c("a", "b", "c") >> dates<-1:5 >> >> a.df<-expand.grid(sites=sites, dates=dates) >> a.df$value<-runif(15,0,100) >> a.df<-as.data.frame(a.df) >> >> >> #now, I want to get the average of the >> mean2<-function(df, date){ >> sub.df<-subset(df, df$dates-date<1 & >> df$dates-date>-1 ) >> return(mean(df$value)) >> } >> >> my.df<-data.frame(sites=NA, dates=NA, V1=NA) >> for(a.date in a.df$dates){ >> new.df<-ddply(a.df, "sites", function(df) mean2 (df, a.date)) >> my.df<-merge(my.df, new.df) #doesn't seem to work >> } >> >> my.df >> >> ______________________________________________ >> R-help@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. > > David Winsemius, MD > Heritage Laboratories > West Hartford, CT > > ______________________________________________ > R-help@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. > ______________________________________________ R-help@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.