sHour <- seq(from = as.POSIXct("2018-01-05 0:00", tz = "UTC"),
to = as.POSIXct("2018-01-13 23:00", tz = "UTC"),
by = "hour")
dayWeek <- strftime(sHour, format = "%u")
My_holidays <- as.POSIXct(c("2018-01-06", "2018-01-09"), tz = "UTC")
星期一是1 星期日是7 如何在dayWeek中将My_holidays添加为7?
所需的输出
> dayWeek
"5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
对于也以My_holidays为单位的sHour中的每个日期(小时),我想将变量7的值从1,2,3等更改。 所以每个假期和星期天将是7
当前输出
> dayWeek
"5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "5" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "6" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "7" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
答案 0 :(得分:0)
像这样吗?
library( dplyr )
df <- data.table( days = sHour ) %>%
mutate( holiday = ifelse( as.Date(days) %in% as.Date(My_holidays), "yes", "no"))
# days holiday
# ...
# 93 2018-01-08 20:00:00 no
# 94 2018-01-08 21:00:00 no
# 95 2018-01-08 22:00:00 no
# 96 2018-01-08 23:00:00 no
# 97 2018-01-09 00:00:00 yes
# 98 2018-01-09 01:00:00 yes
# 99 2018-01-09 02:00:00 yes
# 100 2018-01-09 03:00:00 yes
# 101 2018-01-09 04:00:00 yes
# 102 2018-01-09 05:00:00 yes
# 103 2018-01-09 06:00:00 yes
# 104 2018-01-09 07:00:00 yes
# 105 2018-01-09 08:00:00 yes
# 106 2018-01-09 09:00:00 yes
# 107 2018-01-09 10:00:00 yes
# 108 2018-01-09 11:00:00 yes
# 109 2018-01-09 12:00:00 yes
# 110 2018-01-09 13:00:00 yes
# 111 2018-01-09 14:00:00 yes
# 112 2018-01-09 15:00:00 yes
# 113 2018-01-09 16:00:00 yes
# 114 2018-01-09 17:00:00 yes
# 115 2018-01-09 18:00:00 yes
# 116 2018-01-09 19:00:00 yes
# 117 2018-01-09 20:00:00 yes
# 118 2018-01-09 21:00:00 yes
# 119 2018-01-09 22:00:00 yes
# 120 2018-01-09 23:00:00 yes
# 121 2018-01-10 00:00:00 no
# 122 2018-01-10 01:00:00 no
# ...
答案 1 :(得分:0)
这本质上是一个join
操作,几个R软件包可以提供帮助。我碰巧很喜欢data.table
。
R> dt <- data.table(myhour=seq(from = as.POSIXct("2018-01-05 0:00", tz = "UTC"),
+ to = as.POSIXct("2018-01-13 23:00", tz = "UTC"),
+ by = "hour"))
R> dt[, weekday := wday(myhour)]
R> dt[]
myhour weekday
1: 2018-01-05 00:00:00 6
2: 2018-01-05 01:00:00 6
3: 2018-01-05 02:00:00 6
4: 2018-01-05 03:00:00 6
5: 2018-01-05 04:00:00 6
---
212: 2018-01-13 19:00:00 7
213: 2018-01-13 20:00:00 7
214: 2018-01-13 21:00:00 7
215: 2018-01-13 22:00:00 7
216: 2018-01-13 23:00:00 7
R>
这基本上就是您所拥有的,但是作为data.table
使用其辅助功能wday()
可以在Date
或POSIXt
上使用。
R> myholidays <- as.Date(c("2018-01-06", "2018-01-09")) # no UTC conversion
大致相同,但我们将它们保留为Date
,因为它们是日期对象。
R> dt[, isholiday:=FALSE][as.Date(myhour) %in% myholidays, isholiday:=TRUE][]
myhour weekday isholiday
1: 2018-01-05 00:00:00 6 FALSE
2: 2018-01-05 01:00:00 6 FALSE
3: 2018-01-05 02:00:00 6 FALSE
4: 2018-01-05 03:00:00 6 FALSE
5: 2018-01-05 04:00:00 6 FALSE
---
212: 2018-01-13 19:00:00 7 FALSE
213: 2018-01-13 20:00:00 7 FALSE
214: 2018-01-13 21:00:00 7 FALSE
215: 2018-01-13 22:00:00 7 FALSE
216: 2018-01-13 23:00:00 7 FALSE
这是关键,我们结合了三个data.table
操作符。首先,我们创建
isholiday
作为布尔值,默认为FALSE
。我们将其设置为假日向量中的日期-从POSIXct
到Date
的转换是即时进行的。最后,我们打印结果。
R> head(dt[ isholiday==TRUE, ])
myhour weekday isholiday
1: 2018-01-06 00:00:00 7 TRUE
2: 2018-01-06 01:00:00 7 TRUE
3: 2018-01-06 02:00:00 7 TRUE
4: 2018-01-06 03:00:00 7 TRUE
5: 2018-01-06 04:00:00 7 TRUE
6: 2018-01-06 05:00:00 7 TRUE
R> summary(dt)
myhour weekday isholiday
Min. :2018-01-05 00:00:00 Min. :1.00 Mode :logical
1st Qu.:2018-01-07 05:45:00 1st Qu.:3.00 FALSE:168
Median :2018-01-09 11:30:00 Median :5.00 TRUE :48
Mean :2018-01-09 11:30:00 Mean :4.56
3rd Qu.:2018-01-11 17:15:00 3rd Qu.:6.00
Max. :2018-01-13 23:00:00 Max. :7.00
R>
检查无痛。我们希望两天的每小时数据达到48个条目。