PaulaSpinola
PaulaSpinola

Reputation: 531

How to format monthly variable from year/month numeric variable (R)

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

Answers (2)

ThomasIsCoding
ThomasIsCoding

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

akrun
akrun

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

Related Questions