我试图仅根据日期过滤行,直到响应为“是” 的行,即首先是或最早是。任何帮助将不胜感激。
Date <- c("1/2/2018", "1/3/2018", "1/4/2018", "1/5/2018", "1/6/2018", "1/7/2018", "1/8/2018")
Response <- c("No", "No", "No", "Yes", "No", "No", "Yes")
df <- cbind(Date, Response)
答案 0 :(得分:2)
假设您想保留第一个“是”响应,则可以执行以下操作:
library(dplyr)
df <- data.frame(Date, Response, stringsAsFactors = FALSE)
df %>%
filter(cumsum(lag(Response, default = "") == "Yes") < 1)
Date Response
1 1/2/2018 No
2 1/3/2018 No
3 1/4/2018 No
4 1/5/2018 Yes