gpt4 book ai didi

R比较前几行的值

转载 作者:行者123 更新时间:2023-12-04 10:30:53 26 4
gpt4 key购买 nike

我有这样的数据:

Incident.ID.. = c(rep("INCFI0000029582",4), rep("INCFI0000029587",4))
date = c("2014-09-25 08:39:45", "2014-09-25 08:39:48", "2014-09-25 08:40:44", "2014-10-10 23:04:00", "2014-09-25 08:33:32", "2014-09-25 08:34:41", "2014-09-25 08:35:24", "2014-10-10 23:04:00")
status = c("assigned", "in.progress", "resolved", "closed", "assigned", "resolved", "resolved", "closed")
date.diff=c (3, 56, 1347796,0 ,69 ,43, 1348116, 0)
df = data.frame(Incident.ID..,date, status, date.diff, stringsAsFactors = FALSE)

df
Incident.ID.. date status date.diff
1 INCFI0000029582 2014-09-25 08:39:45 assigned 3
2 INCFI0000029582 2014-09-25 08:39:48 in.progress 56
3 INCFI0000029582 2014-09-25 08:40:44 resolved 1347796
4 INCFI0000029582 2014-10-10 23:04:00 closed 0
5 INCFI0000029587 2014-09-25 08:33:32 assigned 69
6 INCFI0000029587 2014-09-25 08:34:41 resolved 43
7 INCFI0000029587 2014-09-25 08:35:24 resolved 1348116
8 INCFI0000029587 2014-10-10 23:04:00 closed 0

我只想为某个事件选择状态为“已解决”的行。 “已解决”或仅“已关闭”行,所以这就是为什么在进行比较时 Incident.ID.. 必须相同的原因)。

例如,在此示例数据中,只会选择这一行:

6 INCFI0000029587 2014-09-25 08:34:41    resolved        43 

那么我该如何完成呢?

最佳答案

这是一个简单的方法,使用 dplyr 按事件 ID 对数据进行分组,然后使用“lead”函数过滤(选择行)以查看下一行:

library(dplyr)
df %>%
group_by(Incident.ID..) %>%
filter(status == "resolved" & lead(status) != "closed") # you can add %>% ungroup() if required
#Source: local data frame [1 x 4]
#Groups: Incident.ID..
#
# Incident.ID.. date status date.diff
#1 INCFI0000029587 2014-09-25 08:34:41 resolved 43

关于R比较前几行的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30395837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com