DrewConway
DrewConway

Reputation: 5457

Creating a switch-statement

Recently, I needed to convert a series of dates from the form "July 25, 2010" to the standard R format "2010-07-25":

> head(old.data$dateoccurred)`
[1] "April 9, 2004"     "September 1, 2004" "February 7, 2005

To solve this, I would like to make a 12-case switch-statement that returns the the numeric representation of a month; however, R does not support a native switch (at least, I could not find one).

As such, what is the most R-like way to create a switch?

Upvotes: 0

Views: 805

Answers (2)

John
John

Reputation: 23758

DrewConway nicely answers the switch question. The underlying date question has functions specifically for it. strptime() will convert the string to a standard time object which you can format as anything you like.

strptime("September 1, 2004", "%B %d, %Y")

[1] "2004-09-01"

(of course the string constant in the expression above can be replaced with your vector of strings)

Upvotes: 5

DrewConway
DrewConway

Reputation: 5457

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

Upvotes: 1

Related Questions