确定某个值是早于还是晚于时间

问题描述 投票:0回答:1

我有以下数据:

times <- c("20:00","17:00")
df <- data.frame(times)
df$times <- as.POSIXct(df$times, format = '%H:%M')

现在,我想创建一个额外的规则来检测时间是否早于下午 5 点。因此我尝试过:

df$off_hours <- ifelse(df$times < "17:00", 1, 0)

但这给了我:

“as.POSIXlt.character(x, tz, ...) 中的错误: 字符串不是标准的明确格式”

对于我如何改进这一点有什么想法吗?

r datetime time
1个回答
0
投票

您需要将“17:00”转换为时间才能进行比较:

cutoff_time <- as.POSIXct("17:00", format = '%H:%M')

df$off_hours <- ifelse(df$times < cutoff_time, 1, 0)
© www.soinside.com 2019 - 2024. All rights reserved.