Search code examples
rbigdataeconomics

How to convert annual data to monthly data using R?


I have year-wise annual data of GDP of 15 years from 2000-2015. I want to convert this data to monthly data, which only having month and year.

I just want to copy the value of that year to all the months. How can I do this in R. e.g. in year 2010 value is 1708. I want to copy the same value for all the months of 2010.

my data :

> str(gdpnew)
'data.frame':   16 obs. of  3 variables:
 $ X    : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Date : Date, format: "2000-12-31" "2001-12-31" "2002-12-31" ...
 $ Value: num  477 494 524 618 722 ...

> head(gdpnew)
  X       Date   Value
1 1 2000-12-31 476.636
2 2 2001-12-31 493.934
3 3 2002-12-31 523.768
4 4 2003-12-31 618.369
5 5 2004-12-31 721.589
6 6 2005-12-31 834.218
> 

Solution

  • Apparently you want a cross join:

    DT <- read.table(text = "  X       Date   Value
    1 1 2000-12-31 476.636
    2 2 2001-12-31 493.934
    3 3 2002-12-31 523.768
    4 4 2003-12-31 618.369
    5 5 2004-12-31 721.589
    6 6 2005-12-31 834.218", header = TRUE)
    
    library(data.table)
    setDT(DT)
    
    DT[, Date := as.IDate(Date)]
    DT[, year := year(Date)]
    setkey(DT, year)
    DT1 <- DT[CJ(year, month = 1:12)]
    print(DT1)
    
    #    X       Date   Value year month
    # 1: 1 2000-12-31 476.636 2000     1
    # 2: 1 2000-12-31 476.636 2000     2
    # 3: 1 2000-12-31 476.636 2000     3
    # 4: 1 2000-12-31 476.636 2000     4
    # 5: 1 2000-12-31 476.636 2000     5
    # 6: 1 2000-12-31 476.636 2000     6
    # 7: 1 2000-12-31 476.636 2000     7
    # 8: 1 2000-12-31 476.636 2000     8
    # 9: 1 2000-12-31 476.636 2000     9
    #10: 1 2000-12-31 476.636 2000    10
    #11: 1 2000-12-31 476.636 2000    11
    #12: 1 2000-12-31 476.636 2000    12
    #13: 2 2001-12-31 493.934 2001     1
    #14: 2 2001-12-31 493.934 2001     2
    #15: 2 2001-12-31 493.934 2001     3
    #16: 2 2001-12-31 493.934 2001     4
    #17: 2 2001-12-31 493.934 2001     5
    #18: 2 2001-12-31 493.934 2001     6
    #19: 2 2001-12-31 493.934 2001     7
    #20: 2 2001-12-31 493.934 2001     8
    #21: 2 2001-12-31 493.934 2001     9
    #22: 2 2001-12-31 493.934 2001    10
    #23: 2 2001-12-31 493.934 2001    11
    #24: 2 2001-12-31 493.934 2001    12
    #25: 3 2002-12-31 523.768 2002     1
    #26: 3 2002-12-31 523.768 2002     2
    #27: 3 2002-12-31 523.768 2002     3
    #28: 3 2002-12-31 523.768 2002     4
    #29: 3 2002-12-31 523.768 2002     5
    #30: 3 2002-12-31 523.768 2002     6
    #31: 3 2002-12-31 523.768 2002     7
    #32: 3 2002-12-31 523.768 2002     8
    #33: 3 2002-12-31 523.768 2002     9
    #34: 3 2002-12-31 523.768 2002    10
    #35: 3 2002-12-31 523.768 2002    11
    #36: 3 2002-12-31 523.768 2002    12
    #37: 4 2003-12-31 618.369 2003     1
    #38: 4 2003-12-31 618.369 2003     2
    #39: 4 2003-12-31 618.369 2003     3
    #40: 4 2003-12-31 618.369 2003     4
    #41: 4 2003-12-31 618.369 2003     5
    #42: 4 2003-12-31 618.369 2003     6
    #43: 4 2003-12-31 618.369 2003     7
    #44: 4 2003-12-31 618.369 2003     8
    #45: 4 2003-12-31 618.369 2003     9
    #46: 4 2003-12-31 618.369 2003    10
    #47: 4 2003-12-31 618.369 2003    11
    #48: 4 2003-12-31 618.369 2003    12
    #49: 5 2004-12-31 721.589 2004     1
    #50: 5 2004-12-31 721.589 2004     2
    #51: 5 2004-12-31 721.589 2004     3
    #52: 5 2004-12-31 721.589 2004     4
    #53: 5 2004-12-31 721.589 2004     5
    #54: 5 2004-12-31 721.589 2004     6
    #55: 5 2004-12-31 721.589 2004     7
    #56: 5 2004-12-31 721.589 2004     8
    #57: 5 2004-12-31 721.589 2004     9
    #58: 5 2004-12-31 721.589 2004    10
    #59: 5 2004-12-31 721.589 2004    11
    #60: 5 2004-12-31 721.589 2004    12
    #61: 6 2005-12-31 834.218 2005     1
    #62: 6 2005-12-31 834.218 2005     2
    #63: 6 2005-12-31 834.218 2005     3
    #64: 6 2005-12-31 834.218 2005     4
    #65: 6 2005-12-31 834.218 2005     5
    #66: 6 2005-12-31 834.218 2005     6
    #67: 6 2005-12-31 834.218 2005     7
    #68: 6 2005-12-31 834.218 2005     8
    #69: 6 2005-12-31 834.218 2005     9
    #70: 6 2005-12-31 834.218 2005    10
    #71: 6 2005-12-31 834.218 2005    11
    #72: 6 2005-12-31 834.218 2005    12
    #    X       Date   Value year month
    

    You might need to set allow.cartesian = TRUE.