[R] Conditionally adding a constant
vioravis
vioravis at gmail.com
Mon Jan 2 13:10:22 CET 2012
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.
More information about the R-help
mailing list