Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Create column based on previously observed value

Tags:

dataframe

r

I would like to create a new column which reports the reported col1 value larger than zero until a new col1 value larger than zero is encountered (see col2 in df2). I.e. the value of zero in col1 is replaced by the observed value larger than zero.

ID = c(1,1,1,1,1,1,1,1,2,2,2,2)
col1 = c(500,0,0,0,600,0,0,0,450,0,0,0)
df1 = data.frame(ID,col1)

ID = c(1,1,1,1,1,1,1,1,2,2,2,2)
col1 = c(500,0,0,0,600,0,0,0,450,0,0,0)
col2 = c(500,500,500,500,600,600,600,600,450,450,450,450)
df2 = data.frame(ID,col1,col2)

Any way of doing this?

like image 388
ykl Avatar asked Dec 19 '25 07:12

ykl


1 Answers

We can use data.table with zoo. Convert the 'data.frame' to 'data.table' (setDT(df1)), assign a new column 'col2' with the values of 'col1', change the elements which are '0' to NA and then use na.locf to replace the NA elements with the previous non-NA element grouped by "ID".

library(zoo)
library(data.table)
setDT(df1)[, col2:=col1][col2==0, col2:= NA]
df1[,col2:= na.locf(col2) ,ID]
df1
#    ID col1 col2
# 1:  1  500  500
# 2:  1    0  500
# 3:  1    0  500
# 4:  1    0  500
# 5:  1  600  600
# 6:  1    0  600
# 7:  1    0  600
# 8:  1    0  600
# 9:  2  450  450
#10:  2    0  450
#11:  2    0  450
#12:  2    0  450
like image 140
akrun Avatar answered Dec 21 '25 23:12

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!