My data have this format and the frame named ex:
Day value1 value2
12-12-2012 00:00:00 1 1
12-12-2012 00:00:02 NA 2
12-12-2012 00:00:03 1 NA
12-12-2012 00:00:04 NA 4
12-12-2012 00:00:00 2 1
12-12-2012 00:00:02 NA 2
12-12-2012 00:00:03 2 NA
12-12-2012 00:00:04 NA 4
The output I want is:
Day value1 value2
12-12-2012 00:00:00 1 1
12-12-2012 00:00:02 1 2
12-12-2012 00:00:03 1 NA
12-12-2012 00:00:04 1 4
12-12-2012 00:00:00 2 1
12-12-2012 00:00:02 2 2
12-12-2012 00:00:03 2 NA
12-12-2012 00:00:04 2 4
What I tried:
fl = na.locf(ex$value1)
However the problem is that delete the value2. How can I use na.locf in a specific column in a frame with multiple columns with delete them?
require(zoo)
dat <- read.table(text="
Day value1 value2
12-12-2012 00:00:00 1 1
12-12-2012 00:00:02 NA 2
12-12-2012 00:00:03 1 NA
12-12-2012 00:00:04 NA 4
12-12-2012 00:00:00 2 1
12-12-2012 00:00:02 NA 2
12-12-2012 00:00:03 2 NA
12-12-2012 00:00:04 NA 4",header=TRUE, row.names=NULL)
dat$value1 <- na.locf(dat$value1)
I've tried to reproduce your problem, but for me you suggested solution works as you desire.
> require(zoo)
> ex <-data.frame(
Day =c("12-12-2012 00:00:00","12-12-2012 00:00:02","12-12-2012 00:00:03","12-12-2012 00:00:04","12-12-2012 00:00:00","12-12-2012 00:00:02","12-12-2012 00:00:03","12-12-2012 00:00:04"),
value1 = c(1,NA,1,NA,2,NA,2,NA),
value2 = c(1,2,3,4,1,2,3,4))
> fl = na.locf(ex)
> fl
Day value1 value2
1 12-12-2012 00:00:00 1 1
2 12-12-2012 00:00:02 1 2
3 12-12-2012 00:00:03 1 3
4 12-12-2012 00:00:04 1 4
5 12-12-2012 00:00:00 2 1
6 12-12-2012 00:00:02 2 2
7 12-12-2012 00:00:03 2 3
8 12-12-2012 00:00:04 2 4
Perhaps something else is awry?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With