Convert a month abbreviation to a numeric month, in R

Viewed 16393

I'm trying to write a function to convert 3-letter month abreviations to numeric values in R.

Here's what I have, I was wondering if there's a better way to do this:

numMonth <- function(x) {
    months <- list(jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12)
    x <- tolower(x)
    sapply(x,function(x) months[[x]])
}

numMonth(c('JAN','DEC'))
2 Answers
Related