Reputation: 531
I have a vector with numeric (not formated) year/month variable. Consider the vector below for all months of year 2012. How could I create a formated monthly variable from it?
ym <- c(201201,201202,201203,201204,201205,201206,201207,201208,201209,201210,201211,201212)
Upvotes: 0
Views: 173
Reputation: 102920
We can use %%
, e.g.,
> month.abb[ym %% 100]
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
Upvotes: 1
Reputation: 887991
May be need
library(zoo)
format(as.yearmon(as.character(ym), '%Y%m'), '%b')
Or in base R
format(as.Date(paste0(ym, '01'), '%Y%m%d'), '%b')
Upvotes: 2