Ernie Tedeschi
2012-Jul-08 03:20 UTC
[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]]
Hi Ernie, I'll use the built in mtcars dataset for demonstrative purposes. You have some condition, which can be used to create an index ## show data frame mtcars ## create index based on your condition i <- which(mtcars$carb == 1) ## set those rows of mtcars in your index ## to the index - 1 mtcars[i, ] <- mtcars[i - 1, ] ## show resulting data frame mtcars note that if your condition grabs the first row, this will wreck havoc. Cheers, Josh On Sat, Jul 7, 2012 at 8:20 PM, Ernie Tedeschi <ernie.tedeschi at gmail.com> wrote:> 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.-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
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.