Home > Software engineering >  Converting Month abbreviations to numeric
Converting Month abbreviations to numeric

Time:12-17

I'm working with a dataset that includes dates in the following format:

[1] "19-Mar-10" "8-Feb-11" "19-Feb-11" "20-Feb-11" "20-Feb-11" "20-Feb-11" "20-Feb-11" "21-Feb-11"

I'm trying to use difftime to find the difference between two such columns. How would I go about converting the months to a numeric value to achieve this? Thanks in advance for any responses.

CodePudding user response:

in base R:

vec <- c("19-Mar-10", "8-Feb-11", "19-Feb-11", "20-Feb-11",
         "20-Feb-11", "20-Feb-11", "20-Feb-11", "21-Feb-11")

d <- as.Date(vec, format="%d-%b-%y")
d
[1] "2010-03-19" "2011-02-08" "2011-02-19" "2011-02-20"   
[5] "2011-02-20" "2011-02-20" "2011-02-20" "2011-02-21"

as.numeric(format(d, "%m"))
[1] 3 2 2 2 2 2 2 2

# Note that once data are in date format you can do arithmetic on them
# directly:
d[2]
[1] "2011-02-08"
d[1]
[1] "2010-03-19"
d[2]-d[1]
Time difference of 326 days

CodePudding user response:

This is one solution:

library(lubridate)

vec <- "19-Mar-10"

dmy(vec)
#> [1] "2010-03-19"
  •  Tags:  
  • r
  • Related