[R] Notation for previous observation in a data frame
arun
smartpink111 at yahoo.com
Sun Jul 8 08:29:46 CEST 2012
Hi,
Try this:
dat1<-data.frame(var1=15:25)
dat1$var2<-NA
dat1$var2[2:11]<-dat1$var1[1:10]
dat1
var1 var2
1 15 NA
2 16 15
3 17 16
4 18 17
5 19 18
6 20 19
7 21 20
8 22 21
9 23 22
10 24 23
11 25 24
You can also do the same within the single variable.
e.g. dat1$var1[6:10]<-dat1$var[5:9]
A.K.
----- Original Message -----
From: Ernie Tedeschi <ernie.tedeschi at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Saturday, July 7, 2012 11:20 PM
Subject: [R] Notation for previous observation in a data frame
I've created a data frame in R, but in order to clean up some of the data,
I need to set certain variable observations equal to the value of their
previous observation (it would be conditional, but that part's less
important right now). In Stata, I would simply set var = var[_n-1] in those
cases. What is the R equivalent?
[[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.
More information about the R-help
mailing list