Converting day of week to number in R

Raleigh L. picture Raleigh L. · Oct 8, 2015 · Viewed 9.7k times · Source

I'm having trouble converting a .csv column of data with weekdays to a number (so that 1 = Monday, 2 = Tuesday, 3 = Wednesday, etc). I'm trying to use the strptime feature as shown here: http://www.inside-r.org/r-doc/base/strftime

Since I want to convert the weekday to a number, I used the "%u" formatting option. Here's my code below:

> newweekdaynum <- strptime(SFCrimeData$DayOfWeek, "%u")

where SFCrimeData is a data set I have that has a bunch of crime information. No errors come up after I run the statement, but when I want to print "newweekdaynum" all that comes is a huge table of values that all say "NA".

What am I doing wrong?

Answer

Roland picture Roland · Oct 8, 2015

strptime can be used if you have something that can be resolved into a full date/datetime. It will return a datetime object. That's not what you want.

Instead you can make use of ordered factors:

#some example data
set.seed(42)
x <- factor(sample(c("Monday", "Tuesday", "Wednesday", 
                     "Thursday", "Friday", "Saturday", "Sunday"),
            20, TRUE))
# [1] Sunday    Sunday    Wednesday Saturday  Friday    Thursday  Saturday  Monday    Friday    Friday    Thursday  Saturday  Sunday   
#[14] Tuesday   Thursday  Sunday    Sunday    Monday    Thursday  Thursday 
#Levels: Friday Monday Saturday Sunday Thursday Tuesday Wednesday

#turn into ordered factor
x <- factor(x, levels = c("Monday", "Tuesday", "Wednesday", 
                          "Thursday", "Friday", "Saturday", "Sunday"),
            ordered = TRUE)
#[1] Sunday    Sunday    Wednesday Saturday  Friday    Thursday  Saturday  Monday    Friday    Friday    Thursday  Saturday  Sunday   
#[14] Tuesday   Thursday  Sunday    Sunday    Monday    Thursday  Thursday 
#Levels: Monday < Tuesday < Wednesday < Thursday < Friday < Saturday < Sunday

#extract underlying integer values
as.integer(x)
#[1] 7 7 3 6 5 4 6 1 5 5 4 6 7 2 4 7 7 1 4 4

(You wouldn't really need to make it an ordered factor, a factor with the levels specified in the correct order would be sufficient, but weekdays are conceptionally an ordered factor.)