[R] df with max function applied to 6 lags of a variable?!?
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Apr 8 01:22:44 CEST 2011
On Thu, Apr 7, 2011 at 4:25 PM, Rita Carreira <ritacarreira at hotmail.com> wrote:
> Thanks so much for letting me know about the zoo package. However, I don't
> know how to make it work very well. I was able to get the lags computed but
> it assumes that my matrix is made up of just a long variable rather than 136
> different variables. So, I have 270 observations, once I compute the maxima
> of the lags, I should get a 270 x 136 matrix with blanks (NA) in the first 6
> rows. What I actually get after using rollmax is a vector of 36,856
> observations.
> I also tried to run the code you gave me for BOD and I get the following
> error:
>
>> BOD
> Time demand
> 1 1 8.3
> 2 2 10.3
> 3 3 19.0
> 4 4 16.0
> 5 5 15.6
> 6 7 19.8
>> testBOD <- as.data.frame(rollmax(BOD, 3, na.pad = TRUE, align = "right"))
> Error in `[.data.frame`(x, (i - k + 1):i) : undefined columns selected
>
> I noticed that rollmax will work on a matrix but it does not seem to work on
> a data frame. Am I correct?
>
I must have had the development version of zoo loaded at the time.
Try it with that:
> install.packages("zoo", repos = "http://r-forge.r-project.org")
...various output not of interest...
> library(zoo)
> as.data.frame(rollmax(BOD, 3, na.pad = TRUE, align = "right"))
Time demand
1 NA NA
2 NA NA
3 3 19.0
4 4 19.0
5 5 19.0
6 7 19.8
Alternately convert it to zoo first (this uses the CRAN version of zoo):
> library(zoo)
> as.data.frame(rollmax(zoo(BOD), 3, align = "right", na.pad = TRUE))
Time demand
1 NA NA
2 NA NA
3 3 19.0
4 4 19.0
5 5 19.0
6 7 19.8
>
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list