David L. Van Brunt, Ph.D.
2005-Oct-09 19:14 UTC
[R] Insert value from same column of another row (lag across observations)
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? -- --------------------------------------- David L. Van Brunt, Ph.D. mailto:dlvanbrunt@gmail.com [[alternative HTML version deleted]]
Marc Schwartz
2005-Oct-09 21:44 UTC
[R] Insert value from same column of another row (lag across observations)
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
Apparently Analagous Threads
- Repost: Examples of "classwt", "strata", and "sampsize" i n randomForest?
- Repost: Examples of "classwt", "strata", and "sampsize" in randomForest?
- Creating new columns inside a loop
- replacing all NA's in a dataframe with zeros...
- R seems to "stall" after several hours on a long series o f analyses... where to start?