Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting day of week to number in R

Tags:

r

weekday

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?

like image 450
Raleigh L. Avatar asked Oct 08 '15 06:10

Raleigh L.


Video Answer


2 Answers

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.)

like image 97
Roland Avatar answered Sep 30 '22 11:09

Roland


df$Date <- as.Date(df$Date)  
df$wkdaynum <- format(df$Date,"%u")  
df$wkdaynum <- as.numeric(df$wkdaynum)

So, your mistake was to use strptime() instead of format().

like image 36
Mariza Costa-Cabral Avatar answered Sep 30 '22 10:09

Mariza Costa-Cabral