R 如何在特定行之后查看n行
我正在尝试跟踪用户的操作,但我想看看他们在特定事件之后做了什么。如何获得下n行数 例如,我想知道用户在“得到蘑菇”后做了什么,看看他们是否在吃蘑菇。我想为每个用户引用“Get Muslium”,然后查看接下来的几行R 如何在特定行之后查看n行,r,dplyr,R,Dplyr,我正在尝试跟踪用户的操作,但我想看看他们在特定事件之后做了什么。如何获得下n行数 例如,我想知道用户在“得到蘑菇”后做了什么,看看他们是否在吃蘑菇。我想为每个用户引用“Get Muslium”,然后查看接下来的几行 User Action Bob Enter chapter 1 Bob Attack Bob Jump Bob Get mushroom Bob Open inventory Bob Eat mushroom B
User Action
Bob Enter chapter 1
Bob Attack
Bob Jump
Bob Get mushroom
Bob Open inventory
Bob Eat mushroom
Bob Close inventory
Bob Run
Mary Enter chapter 1
Mary Get mushroom
Mary Attack
Mary Jump
Mary Attack
Mary Open inventory
Mary Close inventory
在按用户分组后,我不确定如何处理此问题。如果我想要下面的3行,预期结果将类似于下面的内容
User Action
Bob Get mushroom # Action I want to find and the next 3 lines below it
Bob Open inventory
Bob Eat mushroom
Bob Close inventory
Mary Get mushroom # Action I want to find and the next 3 lines below it
Mary Attack
Mary Jump
Mary Attack
多谢各位 首先使用
which
您可以对每个索引使用lappy
,并使用seq
获得接下来的3个索引
args <- which(df$Action == "Get mushroom")
df[unlist(lapply(args, function(x) seq(x, x+3))), ]
# User Action
#4 Bob Get mushroom
#5 Bob Open inventory
#6 Bob Eat mushroom
#7 Bob Close inventory
#10 Mary Get mushroom
#11 Mary Attack
#12 Mary Jump
#13 Mary Attack
此sapply
解决方案将在数据帧上工作,而不是在数据表上工作,因为它不接受2列矩阵
要使其处理数据表
,可以使用c
df[c(sapply(args, function(x) seq(x, x+3))), ]
首先使用which
您可以对每个索引使用lappy
,并使用seq
获得接下来的3个索引
args <- which(df$Action == "Get mushroom")
df[unlist(lapply(args, function(x) seq(x, x+3))), ]
# User Action
#4 Bob Get mushroom
#5 Bob Open inventory
#6 Bob Eat mushroom
#7 Bob Close inventory
#10 Mary Get mushroom
#11 Mary Attack
#12 Mary Jump
#13 Mary Attack
此sapply
解决方案将在数据帧上工作,而不是在数据表上工作,因为它不接受2列矩阵
要使其处理数据表
,可以使用c
df[c(sapply(args, function(x) seq(x, x+3))), ]
试试这个:
df
User Action
1 Bob Enterchapter1
2 Bob Attack
3 Bob Jump
4 Bob Getmushroom
5 Bob Openinventory
6 Bob Eatmushroom
7 Bob Closeinventory
8 Bob Run
9 Mary Enterchapter1
10 Mary Getmushroom
11 Mary Attack
12 Mary Jump
13 Mary Attack
14 Mary Openinventory
15 Mary Closeinventory
indices <- which(df$Action == 'Getmushroom')
n <- 3
# ensure that x + n does not go beyond the #rows of df
do.call(rbind, lapply(indices, function(x)df[x:min(x+n, nrow(df)),]))
User Action
4 Bob Getmushroom
5 Bob Openinventory
6 Bob Eatmushroom
7 Bob Closeinventory
10 Mary Getmushroom
11 Mary Attack
12 Mary Jump
13 Mary Attack
df
用户操作
1.第1章
2鲍勃攻击
3鲍勃跳
4鲍勃·盖特蘑菇
5.库存
6.蘑菇
7.存货清单
8鲍勃跑
9玛丽:第一章
10玛丽·盖特蘑菇
11玛丽袭击
12玛丽跳
13玛丽袭击
14玛丽开放式库存
15.库存
指数试试这个:
df
User Action
1 Bob Enterchapter1
2 Bob Attack
3 Bob Jump
4 Bob Getmushroom
5 Bob Openinventory
6 Bob Eatmushroom
7 Bob Closeinventory
8 Bob Run
9 Mary Enterchapter1
10 Mary Getmushroom
11 Mary Attack
12 Mary Jump
13 Mary Attack
14 Mary Openinventory
15 Mary Closeinventory
indices <- which(df$Action == 'Getmushroom')
n <- 3
# ensure that x + n does not go beyond the #rows of df
do.call(rbind, lapply(indices, function(x)df[x:min(x+n, nrow(df)),]))
User Action
4 Bob Getmushroom
5 Bob Openinventory
6 Bob Eatmushroom
7 Bob Closeinventory
10 Mary Getmushroom
11 Mary Attack
12 Mary Jump
13 Mary Attack
df
用户操作
1.第1章
2鲍勃攻击
3鲍勃跳
4鲍勃·盖特蘑菇
5.库存
6.蘑菇
7.存货清单
8鲍勃跑
9玛丽:第一章
10玛丽·盖特蘑菇
11玛丽袭击
12玛丽跳
13玛丽袭击
14玛丽开放式库存
15.库存
指数两个具有dplyr
和数据的备选方案。表
:
library(dplyr)
df1 %>%
group_by(User) %>%
slice(rep(which(Action == 'Get-mushroom'), each=4) + 0:3)
library(data.table)
setDT(df1)[df1[, rep(.I[Action == 'Get-mushroom'], each=4) + 0:3, User]$V1]
两者都会导致:
User Action
1: Bob Get-mushroom
2: Bob Open-inventory
3: Bob Eat-mushroom
4: Bob Close-inventory
5: Mary Get-mushroom
6: Mary Attack
7: Mary Jump
8: Mary Attack
具有dplyr
和数据的两个备选方案。表
:
library(dplyr)
df1 %>%
group_by(User) %>%
slice(rep(which(Action == 'Get-mushroom'), each=4) + 0:3)
library(data.table)
setDT(df1)[df1[, rep(.I[Action == 'Get-mushroom'], each=4) + 0:3, User]$V1]
两者都会导致:
User Action
1: Bob Get-mushroom
2: Bob Open-inventory
3: Bob Eat-mushroom
4: Bob Close-inventory
5: Mary Get-mushroom
6: Mary Attack
7: Mary Jump
8: Mary Attack
您可以通过使用sapply(args,…)
@Sotos来避免未列出lappy
,是的,绝对!更新了答案。谢谢。@RonakShah您的第一个取消列表的方法(lapply(args…)有效,但不是sapply版本。知道发生了什么吗?//[.data.table
中出现错误(蘑菇,sapply(args,function(x)seq(x,x+):i是无效类型(矩阵)。也许将来一个2列矩阵可以返回DT元素列表(本着FAQ 2.14中a[B]的精神)。请让datatable帮助了解您是否希望这样做,或者将您的注释添加到FR#657。@antsapply
解决方案在dataframe上工作。您的输入看起来像是一个数据。table
。saaply
的输出是一个2列矩阵,dataframe接受它,但数据似乎不是这样。table
我们需要转换使用c
将其插入一维数组。请参阅更新的帖子。@Ronaksah非常感谢您!我感谢您的帮助,因为我对数据不熟悉,所以我在这个问题上被困了一段时间。table!您可以使用sapply(args,…)避免未列出lappy
@Sotos是的,当然!更新了答案。谢谢。@RonakShah您的第一个取消列表方法(lappy(args…)有效,但Sappy版本无效。知道发生了什么吗?//在[.data.table
中出现错误(蘑菇,Sappy(args,函数(x))seq(x,x+:i是无效类型(矩阵).也许在将来,一个2列矩阵可以返回DT元素列表(按照FAQ 2.14中a[B]的精神)。请让datatable帮助了解您是否希望这样做,或者将您的注释添加到FR#657。@antsapply
解决方案在dataframe上工作。您的输入看起来像是一个数据。table
。saaply
的输出是一个2列矩阵,dataframe接受它,但数据似乎不是这样。table
我们需要转换使用c
将其插入一维数组。请参阅更新的帖子。@RonakShah非常感谢您!我感谢您的帮助,因为我是data.table的新手,所以在这个问题上我被困了一段时间!