查找星期几


215

假设我在R中有一个日期,其日期格式如下。

   date      
2012-02-01 
2012-02-01
2012-02-02

R中有什么方法可以添加与日期关联的星期几的另一列?数据集确实很大,因此手动进行更改并没有意义。

df = data.frame(date=c("2012-02-01", "2012-02-01", "2012-02-02")) 

因此,添加天数后,最终结果将如下所示:

   date       day
2012-02-01   Wednesday
2012-02-01   Wednesday
2012-02-02   Thursday

这可能吗?谁能指出我要允许我这样做的包裹?只是尝试自动按日期生成日期。

Answers:


297
df = data.frame(date=c("2012-02-01", "2012-02-01", "2012-02-02")) 
df$day <- weekdays(as.Date(df$date))
df
##         date       day
## 1 2012-02-01 Wednesday
## 2 2012-02-01 Wednesday
## 3 2012-02-02  Thursday

编辑:只是为了显示另一种方式...

对象的wday组成部分POSIXlt是数字工作日(从星期日开始的0-6)。

as.POSIXlt(df$date)$wday
## [1] 3 3 4

您可以用来子集星期几名称的字符向量

c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
    "Friday", "Saturday")[as.POSIXlt(df$date)$wday + 1]
## [1] "Wednesday" "Wednesday" "Thursday" 

3
+1 weekdays与使用as.POSIXlt?? 一样,有一种方法可以用来获取工作日的数量?
Shambho 2014年

3
@Shambho我想您可以执行此操作:setNames(0:6, c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))[weekdays(as.Date(df$date))]。如果您不喜欢这些名称,则可以将其unname()环绕。
GSee

6
要从您可以执行的日期获取工作日编号(0-6,Sun-Sat):format(as.Date(df $ date),“%w”)。有关格式代码的详细信息,请参见stat.berkeley.edu/~s133/dates.html
JStrahl

65

查找?strftime

%A 当前语言环境中的完整工作日名称

df$day = strftime(df$date,'%A')

15
如果有人搜索工作日号码,请使用- '%u'而不是'%A'
Vlad Holubiev

64

使用lubridate包和功能wday

library(lubridate)
df$date <- as.Date(df$date)
wday(df$date, label=TRUE)
[1] Wed   Wed   Thurs
Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat

11
这种方法的好处是,它返回天作为因子,因此,如果您创建图表,则天数将以正确的顺序排列。
bobfet1

要获取每天的完整单词(例如,星期日而不是星期日):abbr = FALSE
stevec

17

假设您还希望该周从星期一开始(而不是默认为星期日),那么以下内容会有所帮助:

require(lubridate)
df$day = ifelse(wday(df$time)==1,6,wday(df$time)-2)

结果是间隔[0,..,6]中的天数。

如果您希望间隔为[1,.. 7],请使用以下命令:

df$day = ifelse(wday(df$time)==1,7,wday(df$time)-1)

...,或者:

df$day = df$day + 1

5
您也可以使用以下参数week_startwday(df$date, label = TRUE, week_start = 1)
mrub

12

这应该可以解决问题

df = data.frame(date=c("2012-02-01", "2012-02-01", "2012-02-02")) 
dow <- function(x) format(as.Date(x), "%A")
df$day <- dow(df$date)
df

#Returns:
        date       day
1 2012-02-01 Wednesday
2 2012-02-01 Wednesday
3 2012-02-02  Thursday

6
start = as.POSIXct("2017-09-01")
end = as.POSIXct("2017-09-06")

dat = data.frame(Date = seq.POSIXt(from = start,
                                   to = end,
                                   by = "DSTday"))

# see ?strptime for details of formats you can extract

# day of the week as numeric (Monday is 1)
dat$weekday1 = as.numeric(format(dat$Date, format = "%u"))

# abbreviated weekday name
dat$weekday2 = format(dat$Date, format = "%a")

# full weekday name
dat$weekday3 = format(dat$Date, format = "%A")

dat
# returns
    Date       weekday1 weekday2  weekday3
1 2017-09-01        5      Fri    Friday
2 2017-09-02        6      Sat    Saturday
3 2017-09-03        7      Sun    Sunday
4 2017-09-04        1      Mon    Monday
5 2017-09-05        2      Tue    Tuesday
6 2017-09-06        3      Wed    Wednesday

3

JStrahl的表单注释format(as.Date(df$date),"%w"),我们得到当前日期: as.numeric(format(as.Date("2016-05-09"),"%w"))

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.