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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu, 07 Apr 2011 15:41:30 -0400

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:

```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

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help