This answer is inspired by the use of dict
in Python to replicate switch behavior. Rather than have a native switch, create a list
that is indexed by the month names with values for the numeric equivalent:
months<-list("January"="01","February"="02","March"="03","April"="04","May"="05","June"="06","July"="07","August"="08","September"="09","October"="10","November"="11","December"="12")
With this list, it is straightforward to use sapply
to create the conversion:
new.dates<-sapply(as.character(old.data$dateoccurred),function(x) paste(strsplit(x," ")[[1]][3],month.convert(strsplit(x," ")[[1]][1]),sub(",","",strsplit(x," ")[[1]][2]),sep="-"))
Now we have the data formatted the way I wanted.
head(new.data$dateoccurred)
[1] 2004-04-09 2004-09-01 2005-02-07 2005-02-19 2005-02-22 2005-03-11
264 Levels: 2004-04-09 2004-09-01 2005-02-07 2005-02-19 2005-02-22 2005-03-11 2005-03-15 2005-03-19 2005-05-13 2005-06-28 2005-06-29 2005-07-05 ... 2009-12-22