Extract year, month and day from matrix column in one line of code

69 views Asked by At

I'd like to shorten my code.

My current approach is:

year = as.numeric(format(as.Date(matrix[,dt_ind]), format = "%Y"))
month = as.numeric(format(as.Date(matrix[,dt_ind]), format="%m"))
day = as.numeric(format(as.Date(matrix[,dt_ind]), format="%d"))

and my data looks like this:

dput(matrix[1:10,dt_ind]) = c("2018-01-01_0000", "2018-01-01_0000", "2018-01-01_0000", "2018-01-01_0000", 
"2018-01-01_0000", "2018-01-01_0000", "2018-01-01_0000", "2018-01-01_0000", 
"2018-01-01_0000", "2018-01-01_0000")

Is there a better solution?

1

There are 1 answers

0
G. Grothendieck On BEST ANSWER

We refer to the vector shown in the question as v and define it reproducibly in the Note at the end.

1) This gives a data frame whose columns are the required vectors. No packages are used.

read.table(text = v, sep = "-", comment.char = "_", 
  col.names = c("year", "month", "day"))

##    year month day
## 1  2018     1   1
## 2  2018     1   1
...snip...

If you really do want separate vectors in th e global environment or replalce .GlobalEnv with environment() if you want them in the current environment. (These two will be the same if the current environment is the global environment.)

list2env(read.table(text = v, sep = "-", comment.char = "_", 
  col.names = c("year", "month", "day")), .GlobalEnv)

2) The chron package has a function which does this.

library(chron)

month.day.year(v)
## $month
##  [1] 1 1 1 1 1 1 1 1 1 1
##
## $day
##  [1] 1 1 1 1 1 1 1 1 1 1
##
## $year
##  [1] 2018 2018 2018 2018 2018 2018 2018 2018 2018 2018

or this. The point about enviornment() applies here too.

list2env(month.day.year(v), .GlobalEnv)

Note

The input reproducibly:

v <- rep("2018-01-01_0000", 10L)