data.table:在&之前选择n个特定行。在其他行满足条件之后 [英] data.table: Select n specific rows before & after other rows meeting a condition

查看:42
本文介绍了data.table:在&之前选择n个特定行。在其他行满足条件之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下示例数据表:

library(data.table)
DT <- fread("grp y exclude
a 1 0
a 2 0
a 3 0
a 4 1
a 5 0
a 7 1
a 8 0
a 9 0
a 10 0
b 1 0
b 2 0
b 3 0
b 4 1
b 5 0
b 6 1
b 7 1
b 8 0
b 9 0
b 10 0
c 5 1
d 1 0")

我要按组 grp

I want to select


  1. $ c>

  2. 所有具有 y == 5

  3. 的行

  4. ,但是3.仅包含 exclude == 0 的行。

  1. by group grp
  2. all rows that have y==5
  3. and up to two rows before and after each row from 2 within the grouping.
  4. but 3. only those rows that have exclude==0.

假设每个组最多包含一行,其中 y == 5 会产生1.-3的预期结果:

Assuming each group has max one row with y==5, this would yield the desired result for 1.-3.:

idx <- -2:2 # 2 rows before match, the matching row itself, and two rows after match
(row_numbers <- DT[,.I[{
                         x <- rep(which(y==5),each=length(idx))+idx 
                         x[x>0 & x<=.N]
                       }], by=grp]$V1)
# [1]  3  4  5  6  7 12 13 14 15 16 20
DT[row_numbers]
#     grp y exclude
#  1:   a 3       0
#  2:   a 4       1
#  3:   a 5       0 # y==5 + two rows before and two rows after
#  4:   a 7       1
#  5:   a 8       0
#  6:   b 3       0
#  7:   b 4       1
#  8:   b 5       0 # y==5 + two rows before and two rows after
#  9:   b 6       1
# 10:   b 7       1
# 11:   c 5       1 # y==5 + nothing, because the group has only 1 element

但是,我如何合并4.这样我就得到

However, how do I incorporate 4. so that I get

#     grp  y exclude
#  1:   a  2       0
#  2:   a  3       0
#  3:   a  5       0
#  4:   a  8       0
#  5:   a  9       0
#  6:   b  2       0
#  7:   b  3       0
#  8:   b  5       0
#  9:   b  8       0
# 10:   b  9       0
# 11:   c  5       1

?感觉我已经接近了,但是我想我在 head s和 s 中看起来太长了,所以我会感谢一些新的想法。

? Feels like I'm close, but I guess I looked too long at heads and whiches, now, so I'd be thankful for some fresh ideas.

推荐答案

您非常亲密。应该这样做:

You are very close. This should do it:

row_numbers <- DT[exclude==0 | y==5, .I[{
    x <- rep(which(y==5), each=length(idx)) + idx 
    x[x>0 & x<=.N]
  }], by=grp]$V1
DT[row_numbers]

这篇关于data.table:在&amp;之前选择n个特定行。在其他行满足条件之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆