Reputation: 103
I have merged several time series together and I need to subset the data so that the first row has no NAs. Below the first row there can be NAs, even in the entire row. I will have to take care of them through interpolation. Here is a simplified data example:
DF <- data.frame(x = c(NA, NA, 3, 2, 6, NA, 5), y = c(NA, NA, 24, 11, NA, NA, 8), z=c(NA, NA, NA, 33, 22, NA, 19))
date <- c("2001-03-30", "2001-03-31", "2001-04-01", "2001-04-02", "2001-04-03", "2001-04-04", "2001-04-05")
DFz <- zoo(DF, order.by = date)
DFz
x y z
2001-03-30 NA NA NA
2001-03-31 NA NA NA
2001-04-01 3 24 NA
2001-04-02 2 11 33
2001-04-03 6 NA 22
2001-04-04 NA NA NA
2001-04-05 5 8 19
What I'd like to obtain is a new zoo matrix starting from the first row having no NAs, (i.e. from the fourth row in the example above), eventual NA rows below the first row must stay instead. I'd like to obtain this:
2001-04-02 2 11 33
2001-04-03 6 NA 22
2001-04-04 NA NA NA
2001-04-05 5 8 19
I have tried different ways, but I don't really know how to do it. Any suggestions?
Upvotes: 0
Views: 65
Reputation: 269491
Try na.trim
which will dispatch the na.trim.zoo
method:
na.trim(DFz, sides = "left")
giving:
x y z
2001-04-02 2 11 33
2001-04-03 6 NA 22
2001-04-04 NA NA NA
2001-04-05 5 8 19
By default na.trim
trims from both sides so we use sides
to ensure that trailing NAs are kept.
Note: You may wish to familiarize yourself with the various na.*
zoo methods. They should be listed among the output of this command:
apropos("^na[.]")
Upvotes: 2