Re: [R] df with max function applied to 6 lags of a variable?!?

From: Rita Carreira <ritacarreira_at_hotmail.com>
Date: Thu, 07 Apr 2011 20:25:08 +0000

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? Thank you so much for your help!!!
Rita ________________________________________ "If you think education is expensive, try ignorance"--Derek Bok




> From: ggrothendieck@gmail.com
> Date: Thu, 7 Apr 2011 15:41:30 -0400
> Subject: Re: [R] df with max function applied to 6 lags of a variable?!?
> To: ritacarreira_at_hotmail.com
> CC: r-help_at_r-project.org
>
> On Thu, Apr 7, 2011 at 2:29 PM, Rita Carreira <ritacarreira_at_hotmail.com> wrote:
> >
> > Hello everyone!
> > I have a data frame of 136 variables with 270 observations. I would like to create a new data frame such that each element of that data frame contains the maximum value of the 6 prior lags of the initial data frame. So for example, if my original data frame, A, were
> > A1=c(7.72, 7.94, 7.56, 7.54, 0.93, 0.59, 7.21, 8.00, 7.19, 7.57)A2=c(4.27, 3.70, 3.80, 3.67, 3.83, 3.95, 4.02, 2.06, 3.28, 2.17)A3=c(4.16, 2.91, 3.89, 3.73, 4.00, 3.81, 3.86, 1.88, 3.74, 3.75)A4=c(3.56, 3.27, 3.42, 3.32, 3.48, 3.54, 3.60, 2.04, 3.38, 3.37)A5=c(8.24, 8.67, 8.49, 0.00, 9.39, 0.00, 8.49, 8.94, 0.00, 0.00)A6=c(4.28, 4.18, 4.20, 4.14, 4.40, 4.20, 4.20, 4.15, 4.17, 4.16)
> > A <- cbind(A1, A2, A3, A4, A5, A6)
> > My new data frame, Amax, where each element is amax(i,j)=max[a(i-1,j), a(i-2,j), a(i-3,j), a(i-4,j), a(1-5,j), a(i-6,j)] would be:NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 7.94
> > 4.27
> > 4.16
> > 3.56
> > 9.39
> > 4.40 7.94
> > 4.02
> > 4.00
> > 3.60
> > 9.39
> > 4.40 8.00
> > 4.02
> > 4.00
> > 3.60
> > 9.39
> > 4.40 8.00
> > 4.02
> > 4.00
> > 3.60
> > 9.39
> > 4.40 8.00
> > 4.02
> > 4.00
> > 3.60
> > 9.39
> > 4.40
> > So the question is: what is the most efficient way of doing this in R?
> > Thanks!Rita ________________________________________ "If you think education is expensive, try ignorance"--Derek Bok
>
>
> See rollmax in the zoo package and see ?rollmax for the various
> options. e.g. using the built in BOD data frame:
>
> > 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
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
                                               

        [[alternative HTML version deleted]]



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. Received on Thu 07 Apr 2011 - 22:10:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 07 Apr 2011 - 23:50:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive