使用两个数字列过滤data.table行

时间:2016-06-24 12:21:29

标签: r data.table

尝试丢弃V1大于1的行或OR大于0.5

library(data.table)
set.seed(45L)
DT <- data.table(V1=c(1L,2L),
                 V2=LETTERS[1:3],
                 V3=round(rnorm(4),4),
                 V4=1:12)

# tried this approach to get the rows 
DT[ .(V1<1,V3<0.5) ]

Error in bmerge(i, x, leftcols, rightcols, io, xo, roll, rollends, nomatch,  : 
  x.'V2' is a character column being joined to i.'V2' which is type logical'.

# found this solution, but it's a very dirty one. Looking for cleaner approach.
# and being afraid of duplicate rows that meet the two conditions

rbind(DT[ V1<1 ],DT[ V3<0.5 ])

1 个答案:

答案 0 :(得分:-2)

如果要丢弃V1 = 1或V3 = 0.5的行,请使用:

DT = DT[V1>1 & V3<0.5] 

否则,请使用:

DT = DT[V1>=1 & V3<=0.5]