r - Abstracting the year from a date column in a dataframe -
this question has answer here:
- extract month , year zoo::yearmon object 6 answers
i have dataframe date column
date 2008-01-01 2008-01-02 2008-01-03 2008-01-04 how can create new column contains year these dates?
if convert data frame data table
df <- data.table(date = as.date(c('2008-01-01', '2008-01-02', '2008-01-03', '2008-01-04'))) you can use nice assignment , use stringr package year
library(stringr) df[,year := str_match(date, '[0-9]{4}')[,1]]
Comments
Post a Comment