Wasabi
Wasabi

Reputation: 3061

Select rows which match condition and the following row

Let's say I have the following trivial data.table (the real data isn't as predictable):

dt <- data.table(a = c(seq(-5, 5, by = 2),
                       seq(-8, 4, by = 4)))

If I want to select all the negative values, I can simply do dt[a < 0].

    a
1: -5
2: -3
3: -1
4: -8
5: -4

However, what if I want to get all the negative values and the first non-negative value after each sequence of negatives? That is, what if I want the output to be:

    a
1: -5
2: -3
3: -1
4:  1
5: -8
6: -4
7:  0

I can't figure out how to group or use rleid() to get this done.

Upvotes: 1

Views: 48

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

We can use shift with sign to get next row after negative row.

library(data.table)
dt[a < 0 | shift(sign(a) == -1)]

#    a
#1: -5
#2: -3
#3: -1
#4:  1
#5: -8
#6: -4
#7:  0

A shorter approach suggested by OP

dt[a < 0 | shift(a < 0)]

Upvotes: 3

Related Questions