Reputation: 1
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.
Upvotes: 0
Views: 401
Reputation: 732
the strptime function in base R will work for any character to date format including yours... the abbreviations are in the help section of strptime. I agree with gss using the lubridate function if you work regularly with dates.
?strptime
dates<-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")
newdates<-strptime(dates, format = "%y-%b-%d",tz="UTC")
newdates<-as.numeric(newdates)
newdates
Upvotes: 0
Reputation: 4140
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
Upvotes: 3
Reputation: 1453
This is one solution:
library(lubridate)
vec <- "19-Mar-10"
dmy(vec)
#> [1] "2010-03-19"
Upvotes: 1