如何匹配上面一行和一行的字符串

时间:2019-01-15 04:41:40

标签: r

我正在尝试删除以“注销”或“用户身份”开头的“已注销”或“用户身份”条目。
我曾考虑过将这些条目更改为“删除行”以便更轻松地进行设置

我的数据片段是: 登录时间

DateTime            Action                                   Duration    
2018-12-07 10:20:56 User Idled until session expired              439
2018-12-07 11:40:15 User session resumed.                          NA
2018-12-07 15:06:15 Logged Out. Logged in for 292.7 minutes     12360
2018-12-13 13:12:56 Login Successful                               NA
2018-12-13 15:13:43 User Idled until session expired             7247
2018-12-13 16:24:31 Logged Out. Logged in for 263.8 minutes      4248
2018-12-19 07:21:39 Login Successful                               NA

我试图对单元格进行“突变”,但我无法获得正确的行与行之间的匹配。

Logduration <- Logduration %>%  
  mutate(`Action` = ifelse(
  grepl("Logged Out|User Idled", `Action`, ignore.case = TRUE) &
  grepl("Logged Out|User Idled", lag(`Action`), ignore.case = TRUE), 
  "Remove Row", `Action`))

预期结果应该是:

2018-12-07 10:20:56 User Idled until session expired              439
2018-12-07 11:40:15 User session resumed.                          NA
2018-12-07 15:06:15 Logged Out. Logged in for 292.7 minutes     12360
2018-12-13 13:12:56 Login Successful                               NA
2018-12-13 15:13:43 User Idled until session expired             7247
2018-12-13 16:24:31 Remove Row                                   4248
2018-12-19 07:21:39 Login Successful                               NA

1 个答案:

答案 0 :(得分:0)

由于您仅标记了r,所以这里是使用data.table及其shift()函数来处理滞后/超前的解决方案,而不是使用dplyr

Logduration = data.table(Logduration)

Logduration[!(grepl("Logged Out|User Idled", 
                    shift(Action, 1, fill='', "lag"), ignore.case = TRUE) &
              grepl("Logged Out|User Idled", Action, ignore.case = TRUE))]