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

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Mon 10 Oct 2005 - 07:44:35 EST

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Oct 10 07:49:14 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:33:43 EST