Convert a numeric month to a month abbreviation

Solution 1:

Take a look at the month.abb constant. For example, assume you have a vector of integers consisting of the number of the month, then you can use it to get the three letters abbreviation of the month name by doing:

> month <- c(12,3,6,2,3,7)
> month.abb[month]
[1] "Dec" "Mar" "Jun" "Feb" "Mar" "Jul"

Solution 2:

If you need non-standard month abbreviation, then create your own month lookup vector:

#dummy data
df <- data.frame(month = c(1,3,5))
#months vector assuming 1st month is Jan.
mymonths <- c("Jan","Feb","Mar",
              "Apr","May","Jun",
              "Jul","Aug","Sep",
              "Oct","Nov","Dec")
#add abbreviated month name
df$MonthAbb <- mymonths[ df$month ]

#result
df
#   month MonthAbb
# 1     1      Jan
# 2     3      Mar
# 3     5      May