R工作室时间栏在之间

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

我有一个名为“test”的表。在该表中我有一列“时间”。 时间列(作为示例):13:03:36 或 15:40:39 我想要另一列是:13 - 14 或 15 - 16。

谢谢

rstudio
1个回答
0
投票

试试这个:

library(dplyr)

test["Category"] <- if_else(test$Time < "15:00:00", "13-14", "15-16")

这是所提供示例的最简单的解决方案。如果您需要更多类别,则需要更具可扩展性的代码:

test$Category <- sapply(test$Time, function(x) paste0(sub(":\\d\\d:\\d\\d", "", x), "-", as.numeric(sub(":\\d\\d:\\d\\d", "", x))+1)) 
© www.soinside.com 2019 - 2024. All rights reserved.