Looking to format column names to dates after reading in a CSV

Using as.yearmon you can try :

names(df) <- zoo::as.yearmon(names(df), 'X%Y.%m')

Or in base R pasting an arbitrary date :

names(df) <- format(as.Date(paste0(names(df), '.01'), 'X%Y.%m.%d'), '%b-%Y')

As an example :

x <- c('X2017.04', 'X2017.05', 'X2017.06')
format(as.Date(paste0(x, '.01'), 'X%Y.%m.%d'), '%b-%Y')
#[1] "Apr-2017" "May-2017" "Jun-2017"

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top