1
DF1 <- DF[DF$cat == 'A', ]
DF2 <- DF[DF$cat == 'B', ]
RDF <- rbind(DF1, DF2)

Is there a way to express this in a more straightforward way, such as

RDF <- DF[DF$ cat == c('A','B'), ] # Does not work
Matt Dowle
  • 58,872
  • 22
  • 166
  • 224
dmvianna
  • 15,088
  • 18
  • 77
  • 106

3 Answers3

3
RDF <- DF[DF$cat %in% c('A','B'), ]
David Robinson
  • 77,383
  • 16
  • 167
  • 187
1

You can use data.table and keys

library(data.table)
DT <- data.table(DF)
setkey(DT, cat)

DT[c("A", "B"),]
Matt Dowle
  • 58,872
  • 22
  • 166
  • 224
mnel
  • 113,303
  • 27
  • 265
  • 254
1

More generally, for two conditions:

RDF <- DF[ DF$cat == "A" | DF$dog == "B", ]
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
  • Minor point: that repeats the variable name `DF` twice. See here for the issue: http://stackoverflow.com/a/10758086/403310. – Matt Dowle Nov 07 '12 at 10:34