简体   繁体   中英

filter data.table rows using two numerical columns

Trying to discard rows where V1 is greater than 1 OR V3 is greater than 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 ])

If you want to discard the rows where V1 = 1 OR V3 = 0.5, then use:

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

Otherwise, use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM