Let's say I have a dataframe like this:
d <- data.frame(order = c("101", "01", "10", "01", "101"),
dur_1 = c(50, 20, 40, 25, 45),
dur_2 = c(40, 30, 45, 34, 96),
dur_3 = c(20, 0, 0, 0, 125))
What I want to do is make new columns pre_order
and pre_dur
.
pre_order
is the very last character of the previous order
. So the first row should be NA. Because we don't know the previous value of order
pre_dur
is the very last nonnegative value of the previous dur_
column. I want to express as dur_
beacause, in my actual data, I have to deal with a generalized version. So I have to express it as dur_
. My expected output should look like this:
d1<-data.frame(order=c("101","01","10","01","101"),
dur_1=c(50,20,40,25,45),
dur_2=c(40,30,45,34,96),
dur_3=c(20,0,0,0,125),
pre_order=c(NA,1,1,0,1),
pre_dur=c(NA,20,30,45,34))
I don't know how to do that in R