I have several files of data that look like this:
X code year month day pp
1 4515 1953 6 1 0
2 4515 1953 6 2 0
3 4515 1953 6 3 0
4 4515 1953 6 4 0
5 4515 1953 6 5 3.5
Sometimes there is data missing, but I don’t have NAs, the rows simply don’t exist. I need to create NAs when the data is missing. I though I could start by identifying when that occurs by converting it to a zoo object and check for strict regularity (I never used zoo before), I used the following code:
z.date<-paste(CET$year, CET$month, CET$day, sep="/")
z <- read.zoo(CET, order.by= z.date )
reg<-is.regular(z, strict = TRUE)
But the answer is always true!
Can anyone tell me why is not working? Or even better, tell me a way to create NAs when the data is missing (with or without zoo package)?
thanks
The
seqfunction has some interesting features that you can use to easily generate a complete sequence of dates. For example, the following code can be used to generate a sequence of dates starting on April 25:Edit: This feature is documented in
?seq.DateNow use the same principle to generate some data with “missing” rows, by generating the sequence for every 2nd day:
To answer your question, one can use vector subscripting or the
matchfunction to create a dataset with NAs:To combine this result with the original full data: