How to create “NA” for missing data in a time series

后端 未结 4 1198
忘了有多久
忘了有多久 2021-01-30 09:54

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         


        
4条回答
  •  天涯浪人
    2021-01-30 10:33

    The first thing to note is that z.date is character, not Date.

    Here's how I would solve your problem using xts (a subclass of zoo).

    # remove the third obs from sample data
    CET <- CET[-3,]
    # create an actual Date column in CET
    CET$date <- as.Date(with(CET, paste(year, month, day, sep="-")))
    # create an xts object using 'date' column
    x <- xts(CET[,c("code","pp")], CET$date)
    # now merge 'x' with a regular date sequence spanning the start/end of 'x'
    X <- merge(x, timeBasedSeq(paste(start(x), end(x), sep="::")))
    X
    #            code  pp
    # 1953-06-01 4515 0.0
    # 1953-06-02 4515 0.0
    # 1953-06-03   NA  NA
    # 1953-06-04 4515 0.0
    # 1953-06-05 4515 3.5
    

提交回复
热议问题