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

From: Rita Carreira <ritacarreira_at_hotmail.com>
Date: Thu, 07 Apr 2011 18:29:35 +0000

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


 		 	   		  
	[[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 - 18:32:07 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 - 20:10:28 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