r - Create a new column whose formula depends on a cell value of another row -
r - Create a new column whose formula depends on a cell value of another row -
how create new column formula depends on cell value of row
x y z 1 1 10 2 2 20 3 3 30 4 b 1 40
this sample data. want final output follows
x y z prevy 1 1 10 0 2 2 20 10 3 3 30 20 4 b 1 40 0
where prevy z value x=current_x_val , y=current_y_val-1 0 if not available.
how accomplish this.
my progress far :
data[data$x == "a" & data$y==2-1,3]
i manually come in values , prevy each row. how do rows in single shot ?
or data.table
solution (similar mrflick) faster big info set
library(data.table) setdt(dat)[, prevy := c(0, z[-length(z)]), = x]
r
Comments
Post a Comment