r - Split Date into Year and Month in data.table -
given,
set.seed(1234) library(data.table) dt <- data.table(date=c(201405, 201406, 201501, 201503), x = rnorm(4, 0, 1))
return,
date x 1: 201405 -1.2070657 2: 201406 0.2774292 3: 201501 1.0844412 4: 201503 -2.3456977
i want split date year , month follows:
year month x 1: 2014 5 -1.2070657 2: 2014 6 0.2774292 3: 2015 1 1.0844412 4: 2015 3 -2.3456977
how can this?
converting comment answer, use substr
combined assignment reference semantics. can wrap as.integer
if prefer not return string
dt[, `:=`(year = substr(date, 1l, 4l), month = substr(date, 5l, 6l))]
Comments
Post a Comment