r - Abstracting the year from a date column in a dataframe -


this question has answer here:

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

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -