如果我有此数据
Group,start_time
1,9:05:00
1,9:07:00
1,19:09:00
1,9:00:00
1,9:00:00
1,9:02:00
2,9:05:00
2,9:07:00
2,19:09:00
2,9:00:00
2,9:00:00
2,9:02:00
,我想对我的数据进行如下列检查。我怎样才能做到这一点?谢谢
Group,start_time, check
1,9:05:00,True
1,9:07:00,True
1,19:09:00, True
1,9:00:00,False
1,9:00:00,False
1,9:02:00,False
2,9:05:00,True
2,9:07:00,True
2,19:09:00,True
2,9:00:00,False
2,9:00:00,False
2,9:02:00,False
答案 0 :(得分:0)
这是一个可能的解决方案:
df = structure(list(Group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L), start_time = structure(c(4L, 5L, 1L, 2L, 2L, 3L,
4L, 5L, 1L, 2L, 2L, 3L), .Label = c("19:09:00", "9:00:00", "9:02:00",
"9:05:00", "9:07:00"), class = "factor")), class = "data.frame", row.names = c(NA, -12L))
library(dplyr)
df %>%
group_by(Group) %>%
mutate(check = as.numeric(gsub(":","",start_time)) >= cummax(as.numeric(gsub(":","",start_time)))) %>%
ungroup()
# # A tibble: 12 x 3
# Group start_time check
# <int> <fct> <lgl>
# 1 1 9:05:00 TRUE
# 2 1 9:07:00 TRUE
# 3 1 19:09:00 TRUE
# 4 1 9:00:00 FALSE
# 5 1 9:00:00 FALSE
# 6 1 9:02:00 FALSE
# 7 2 9:05:00 TRUE
# 8 2 9:07:00 TRUE
# 9 2 19:09:00 TRUE
#10 2 9:00:00 FALSE
#11 2 9:00:00 FALSE
#12 2 9:02:00 FALSE
我假设FALSE
个案例是我们似乎可以追溯到过去的案例。
为了比较时间,我删除了:
,并使用其余的(数字)字符创建了一个数字。