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?
We refer to the vector shown in the question as
vand 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.
If you really do want separate vectors in th e global environment or replalce
.GlobalEnvwithenvironment()if you want them in the current environment. (These two will be the same if the current environment is the global environment.)2) The chron package has a function which does this.
or this. The point about
enviornment()applies here too.Note
The input reproducibly: