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

后端 未结 4 1231
忘了有多久
忘了有多久 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:25

    I had to deal with the similar problem with a monthly time series. I did it with directly joining two data.table/data.frame by the time variable. My point is that time series is also a kind of datasets. So you can also manipulate any time series as regular dataset in a regular way. Here is my solution:

    library(zoo)    
    (full <- data.table(yrAndMo = as.yearmon(seq(as.Date('2008-01-01'), by = '1 month', length = someLength)))) 
    # the full time horizon that you want to have
    #  yrAndMo
    #  1: Jan 2008
    #  2: Feb 2008
    #  3: Mar 2008
    #  4: Apr 2008
    #  5: May 2008
    # ---         
    # 98: Feb 2016
    # 99: Mar 2016
    # 100: Apr 2016
    # 101: May 2016
    # 102: Jun 2016
    
    exampleDat # the actually data you want to append to the full time horizon
    # yrAndMo someValue
    # 1 Mar 2010      7500
    # 2 Jun 2010      1115
    # 3 Mar 2011      2726
    # 4 Apr 2011      1865
    # 5 Nov 2011      1695
    # 6 Dec 2012     10000
    # 7 Mar 2016      1000
    
    library(plyr)
    join(full, exampleDat, by = 'yrAndMo', type = "left")
    #   yrAndMo someValue
    #   1: Jan 2008        NA
    #   2: Feb 2008        NA
    #   3: Mar 2008        NA
    #   4: Apr 2008        NA
    #   5: May 2008        NA
    #  ---                   
    #  98: Feb 2016        NA
    #  99: Mar 2016      1000
    # 100: Apr 2016        NA
    # 101: May 2016        NA
    # 102: Jun 2016        NA
    

    after this you can easily change the class of the dataset back to any type of time series that you want to have. I preferred read.zoo.

提交回复
热议问题