使用 R 中其他列的 case_when 添加新列

Add new column using case_when of other column in R

我想在我的数据框中添加一列,该列采用“周一”、“周二”、“周三”、“周四”等值,具体取决于包含“日期时间”变量的另一列,但是我无法使用工作日功能,因为它还取决于一天中的时间。

即如果 created_at 列介于:2021-03-01 09:00:00 和 2021-03-02 09:00:00 之间,则新列应归类为“mon”。

如果 created_at 在 2021-03-02 09:00:00 和 2021-03-03 09:00:00 之间,它应该 归类为“星期二”。

本周剩下的时间依此类推。 (按照股市时间)

这应该有效:

library(lubridate)
library(dplyr)

df %>%
  mutate(
    result = wday(created_at - hours(9), label = TRUE)
  )

如果您有问题,请post使用dput()的可重现数据样本。