How to periodize years in terms?

I would like to go from this:

  years
 -------
  1994
  2001
   .
   .

      

For this:

int dates
 ------
  8793    # 1994-01-28
  8824    # 1994-02-28
  8852    # 1994-03-28
  8883    # 1994-04-28
  8913    # 1994-05-28
  8944    # 1994-06-28
  8974    # 1994-07-28
  9005    # 1994-08-28
  9036    # 1994-09-28
  9066    # 1994-10-28
  9097    # 1994-11-28
  9127    # 1994-12-28
 11350    # 2001-01-28
 11381    # 2001-02-28
 11409    # 2001-03-28
 11440    # 2001-04-28
 11470    # 2001-05-28
 11501    # 2001-06-28
 11531    # 2001-07-28
 11562    # 2001-08-28
 11593    # 2001-09-28
 11623    # 2001-10-28
 11654    # 2001-11-28
 11684    # 2001-12-28
   .
   .

      

i.e. periodizing each year by 12 dates (28th of every month of that year) as whole numbers in 1970.

What's the most efficient way to do this?

My attempt is very slow!

require(data.table)

# Sample data
dt <- data.table(year=c(1994,2001)) # edit

# Create results table
data <- data.table(dates=integer())

for (i in 1:12) {
    temp <- dt
    temp$dates <- as.integer(as.Date(paste(temp$year, "-", sprintf( "%02d",i),"-28", sep="")))
    temp <- subset(temp, select=dates)
    data <- rbind(temp,data)
}

# Sort
data <- data[with(data, order(dates)),]

      

+3


source to share


3 answers


Here's a one-liner:

as.integer(as.Date(apply(expand.grid(1:12,c(1994,2001)), 1, 
                         function(x)paste(x[2], x[1], 28,sep="-"))))

 [1]  8793  8824  8852  8883  8913  8944  8974  9005  9036  9066  9097  9127 11350 11381 11409 11440 11470 11501
[19] 11531 11562 11593 11623 11654 11684

      

And here is a step by step explanation:



expand.grid(1:12, c(1994,2001))
 Var1 Var2
1     1 1994
2     2 1994
3     3 1994
4     4 1994
5     5 1994
6     6 1994
7     7 1994
8     8 1994
9     9 1994
10   10 1994
11   11 1994
12   12 1994
13    1 2001
14    2 2001
15    3 2001
16    4 2001
17    5 2001
18    6 2001
19    7 2001
20    8 2001
21    9 2001
22   10 2001
23   11 2001
24   12 2001

      

To what you are apply

for each line function paste()

. Then convert to a Date object, which you then convert to an integer (default based on 1970).

+2


source


Try it. The inputs and outputs are all data tables:



# input data
Y <- data.table(year = c(1994, 2001))
M <- data.table(month = 1:12)

as.data.table( merge.data.frame( M, Y ))[, 
   list(`int dates` = as.numeric(as.Date(ISOdate(year, month, 28))))
]

      

+2


source


If you are going to move this data to excel add "25569" to the numbers in excel and you will get your dates. This is a problem with R, and I use that number to put dates back in a superior format.

-1


source







All Articles