Converting day of week to number in R

时光总嘲笑我的痴心妄想 提交于 2019-12-10 22:37:36

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/33008615/converting-day-of-week-to-number-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!