简体   繁体   English

R - 如何在数据帧中返回所有特定行以下的所有行?

[英]R - How to Return All Rows Below Selected Specific Rows in a Dataframe?

So I have this this data frame with these values 所以这个数据框有这些值

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

I'm trying to get all the pages that are two rows below the rows whose page name is 'New Project' and activity is 'Submit Form' in a new dataframe like this. 我正在尝试获取页面名称为“新建项目”的行下面两行的所有页面,并且活动是在这样的新数据框中的“提交表单”。

page_name         activity
Expenses          View Page
Payment Claims    View Page

I used this R code to get all the rows who follows the conditions I need. 我用这个R代码来获取所有遵循我需要的条件的行。

after_newproj <- with(dat, dat[((page_name == 'New Project' & activity == 'Submit Form')),] )

Now I tried using this to get what I want to happen and it returns the same number of rows but all null. 现在我尝试使用它来获得我想要发生的事情,它返回相同数量的行,但都是null。

after_newproj <- with(dat, dat[((page_name == 'New Project' & activity == 'Submit Form')),] + c(2) )

My solution is that you create additional fields which can then be filtered 我的解决方案是您创建其他字段,然后可以对其进行过滤
Code updated....it now works. 代码更新....它现在有效。

function to help with filtering 功能,以帮助过滤

 global.counter <- 2 fill.filler <- function(x){ if(x == "Break") global.counter <<- 0 else global.counter <<- global.counter + 1 return(global.counter) } 

code to filter out the rows needed 用于过滤掉所需行的代码

 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)) 

It's important the global.counter be set at 2 otherwise the first few rows will also be included in the final selection which you want to avoid. 重要的是global.counter设置为2,否则前几行也将包含在您想要避免的最终选择中。

Hope the code is easy enough to understand. 希望代码很容易理解。

Data 数据

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 solution dplyr解决方案

lead-lag functions of dplyr are helpful in these cases dplyr lead-lag函数在这些情况下dplyr

library(dplyr)
df[lag(df$page_name,2)=="New Project" & lag(df$activity,2)=="Submit Form",]

Output 产量

         page_name  activity
1:        Expenses View Page
2:  Payment Claims View Page

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM