Vitaliy  Radchenko
Vitaliy Radchenko

Reputation: 178

Subset all rows before negative value in group

I have data.table

X = data.table(x = c(1,1,1,1,1,2,2,2,2,2), y = c(3,2,1,-1,5,7,4,-2,3,5))

I want to subset only rows which are above negative values in one group:

res = data.table(x = c(1,1,1,2,2), y = c(3,2,1,7,4)

From five values in first group, I want to get only first three, because fourth is negative, and the same with second group.

Upvotes: 1

Views: 203

Answers (2)

akrun
akrun

Reputation: 887971

We may also do

X[, .SD[cummin(sign(y))>0], x]
#   x y
#1: 1 3
#2: 1 2
#3: 1 1
#4: 2 7
#5: 2 4

Upvotes: 1

talat
talat

Reputation: 70336

Here are two options:

X[, .SD[seq_len(which.max(y<0)-1L)], by = x]

Or (perhaps more efficient because it avoids .SD):

X[ X[, .I[seq_len(which.max(y<0)-1L)], by = x]$V1 ]

Upvotes: 6

Related Questions