[R] Insert value from same column of another row (lag across observations)

Marc Schwartz MSchwartz at mn.rr.com
Sun Oct 9 23:44:35 CEST 2005


On Sun, 2005-10-09 at 14:14 -0500, David L. Van Brunt, Ph.D. wrote:
> I know I've done this before, but it's been a while and I can't find quite
> what I need in the help files or archives.
> 
> I have a text field in a very large data frame. I'd like to add a column
> that represents the value from an existing field, from the next record (the
> data are sorted). I'm trying to represent "what happens tomorrow", so the
> "today" row would have an "NA" value (unknown).
> 
> So if x is:
> > x
> [,1]
> [1,] "today"
> [2,] "yesterday"
> [3,] "the day before"
> [4,] "two days before"
> 
> any y is
> > cbind(x,c(1,2,3,4))
> [,1] [,2]
> [1,] "today" "1"
> [2,] "yesterday" "2"
> [3,] "the day before" "3"
> [4,] "two days before" "4"
> 
> 
> z should become
> 
> [,1] [,2] [,3]
> [1,] "today" "1" "NA"
> [2,] "yesterday" "2" "1"
> [3,] "the day before" "3" "2"
> [4,] "two days before" "4" "3"
> 
> I've tried
> z<-cbind(y,c("NA",y[-1,2]))
> 
> but I get
> [1,] "today" "1" "NA"
> [2,] "yesterday" "2" "2"
> [3,] "the day before" "3" "3"
> [4,] "two days before" "4" "4"
> 
> So the "lagging" doesn't work.
> 
> Any ideas?


You are close, but you are eliminating the first value in y[, 2] by
using '-1' rather than the last:

> z <- cbind(y, c(NA, y[-nrow(y), 2]))

> z
     [,1]              [,2] [,3]
[1,] "today"           "1"  NA
[2,] "yesterday"       "2"  "1"
[3,] "the day before"  "3"  "2"
[4,] "two days before" "4"  "3"

Do not quote the NA, as "NA" is a character vector, as opposed to a
missing value indicator. See ?NA for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list