I am trying to add a constant to the previous value of a variable based on
certain conditions. Maybe there is a simple way to do this that I am missing
completely. I have given an example below:

df <- data.frame(x = c(1,2,3,4,5), y = c(10,20,30,NA,NA))

> df
  x  y
1 1 10
2 2 20
3 3 30
4 4 NA
5 5 NA

I want to add 2 to the previous value of y, if x exceeds 3 (also will have
to handle NAs in the process). The resulting output would look like:

  x  y
1 1 10
2 2 20
3 3 30
4 4 32
5 5 34

Can someone please explain how to do it? Thank you.

Ravi










--
View this message in context: 
http://r.789695.n4.nabble.com/Conditionally-adding-a-constant-tp4253049p4253049.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@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.

Reply via email to