r - How to get values with same time with previous days same value -
how replace na
s in var_1 same previous days value
eg.row 4 has missing value - replace value/na same time in previous day(row 1).
similarly row 7 row 3, row 10 row 9 , row13 row 11. can replace na previous value zoo-na.lacf
id date var var_1 1 21-01-2014 00:15 22 22 2 21-01-2014 00:30 33 33 3 21-01-2014 00:45 13 13 4 22-01-2014 00:15 na 22 5 22-01-2014 00:30 22 22 6 22-01-2014 00:35 54 54 7 22-01-2014 00:45 na 13 8 23-01-2014 00:15 23 23 9 25-01-2104 01:00 34 34 10 26-01-2104 01:00 na 34 11 27-01-2104 02:00 2 2 12 27-01-2104 03:00 21 21 13 28-01-2104 02:00 na 2
one way use format
time, , split on that. use na.locf
fill nas , rbind
together, i.e.
library(zoo) do.call(rbind, c(lapply(split(df, format(df$date, format = '%h:%m')), function(i) { i$var <- na.locf(i$var); }), make.row.names = false))
which gives,
id date var var_1 1 1 2014-01-21 00:15:00 22 22 2 4 2014-01-22 00:15:00 22 22 3 8 2014-01-23 00:15:00 23 23 4 2 2014-01-21 00:30:00 33 33 5 5 2014-01-22 00:30:00 22 22 6 6 2014-01-22 00:35:00 54 54 7 3 2014-01-21 00:45:00 13 13 8 7 2014-01-22 00:45:00 13 13 9 9 2104-01-25 01:00:00 34 34 10 10 2104-01-26 01:00:00 34 34 11 11 2104-01-27 02:00:00 2 2 12 13 2104-01-28 02:00:00 2 2 13 12 2104-01-27 03:00:00 21 21
you can order
on id
if per usualdf[with(df, order(id)),]
wiki
Comments
Post a Comment