這是我的玩具資料框:
df <- data.frame(
date = (1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2),
action =c("ID=1", "foo","bah", "error",
"ID=2", "foo","bah", "success",
"ID=3", "foo","bah", "error",
"ID=4", "foo","bah", "success",
"ID=5", "foo","bah", "success",
"ID=6", "foo","bah", "error",
"ID=7", "foo","bah", "error",
"ID=8", "foo","bah", "success",
"ID=9", "foo","bah", "success",
"ID=10", "foo","bah", "success"
)
)
我想進行處理df,以便每當action列中的條目等于“錯誤”時,包含“ID =”的前一行與資料列中的關聯條目一起回傳。所以預期的結果是:
date action
1 ID=1
2 ID=3
2 ID=6
2 ID=7
我嘗試使用以下內容:
df %>%
filter(str_detect(action,"error")) %>%
slice(-4)
,但它并不完全在那里!
uj5u.com熱心網友回復:
有兩個filters:
library(dplyr)
df %>%
filter(action == "error" | grepl("ID", action)) %>%
filter(lead(action) == "error")
# date action
# 1 1 ID=1
# 2 2 ID=3
# 3 2 ID=6
# 4 2 ID=7
uj5u.com熱心網友回復:
如果原始資料幀保持這個標準的 4 行重復順序,那么這里是一個僅使用基本 R 的單行:
df[which(df$action=="error")-3, ]
date action
1 1 ID=1
9 2 ID=3
21 2 ID=6
25 2 ID=7
uj5u.com熱心網友回復:
我們可能會使用
library(dplyr)
df %>%
filter(lead(action, n = 3) == "error")
date action
1 1 ID=1
2 2 ID=3
3 2 ID=6
4 2 ID=7
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521655.html
標籤:rdplyr片
上一篇:R,dplyr。分組表的總百分比
