JK_chitown
JK_chitown

Reputation: 179

How do I subset rows in data.table that meet some minimum criteria?

I'm a noob to data.table in R and I'd like to skip the last value of z=3 in this example:

> DT = data.table(y=c(1,1,2,2,2,3,3,3,4,4),x=1:10,z=c(1,1,1,1,2,2,2,2,3,3))
> DT[,list(list(predict(smooth.spline(x,y),c(4,5,6))$y)),by=z]

Error in smooth.spline(x, y) : need at least four unique 'x' values

If I simply delete z=3 I get the answer I want:

> DT = data.table(y=c(1,1,2,2,2,3,3,3),x=1:8,z=c(1,1,1,1,2,2,2,2))
> DT[,list(list(predict(smooth.spline(x,y),c(4,5,6))$y)),by=z]

   z                                                  V1
1: 1  2.09999998977689,2.49999997903384,2.89999996829078
2: 2 0.999895853971133,2.04533519691888,2.90932467439562

What a great package!

Upvotes: 1

Views: 3204

Answers (2)

mnel
mnel

Reputation: 115382

If you simply want to omit the results when .N <4, then you can use if (not ifelse). If .N <4, then nothing is returned

DT[,if(.N>=4){ list(list(predict(smooth.spline(x,y),c(4,5,6))$y))},by=z]
#    z                                                  V1
# 1: 1   2.1000000266026,2.50000003412706,2.90000004165153
# 2: 2 0.999895884129996,2.04533520266699,2.90932466433092

Upvotes: 1

Ricardo Saporta
Ricardo Saporta

Reputation: 55340

Omitting the rows were z is 3 is as simple as

DT[z!=3,  <whatever expression you'd like>]

If your data.table is keyed by z then you can use

DT[!.(3), .....]

Upvotes: 3

Related Questions