[英]R - How to Return All Rows Below Selected Specific Rows in a Dataframe?
所以这个数据框有这些值
page_name activity
Home View Page
New Project View Page
New Project Submit Form
New Project View Page
Expenses View Page
Quotes View Page
New Project View Page
New Project Submit Form
New Project View Page
Payment Claims View Page
我正在尝试获取页面名称为“新建项目”的行下面两行的所有页面,并且活动是在这样的新数据框中的“提交表单”。
page_name activity
Expenses View Page
Payment Claims View Page
我用这个R代码来获取所有遵循我需要的条件的行。
after_newproj <- with(dat, dat[((page_name == 'New Project' & activity == 'Submit Form')),] )
现在我尝试使用它来获得我想要发生的事情,它返回相同数量的行,但都是null。
after_newproj <- with(dat, dat[((page_name == 'New Project' & activity == 'Submit Form')),] + c(2) )
我的解决方案是您创建其他字段,然后可以对其进行过滤
代码更新....它现在有效。
global.counter <- 2 fill.filler <- function(x){ if(x == "Break") global.counter <<- 0 else global.counter <<- global.counter + 1 return(global.counter) }
df %>% mutate(fill = if_else(page_name == "New Project" & activity == "Submit Form", "Break", "Count")) %>% mutate(counter = sapply(.$fill, fill.filler)) %>% filter(counter <= 2, activity != "Submit Form") %>% select(-c(fill, counter))
重要的是global.counter设置为2,否则前几行也将包含在您想要避免的最终选择中。
希望代码很容易理解。
library(data.table)
df <- fread("page_name,activity
Home,View Page
New Project,View Page
New Project,Submit Form
New Project,View Page
Expenses,View Page
Quotes,View Page
New Project,View Page
New Project,Submit Form
New Project,View Page
Payment Claims,View Page", sep=",", header=T)
dplyr
lead-lag
函数在这些情况下dplyr
用
library(dplyr)
df[lag(df$page_name,2)=="New Project" & lag(df$activity,2)=="Submit Form",]
page_name activity
1: Expenses View Page
2: Payment Claims View Page
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.