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
  • 57,542
  • 22
  • 163
  • 221
dmvianna
  • 13,786
  • 18
  • 76
  • 103

3 Answers3

3
RDF <- DF[DF$cat %in% c('A','B'), ]
David Robinson
  • 74,512
  • 15
  • 159
  • 179
1

You can use data.table and keys

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

DT[c("A", "B"),]
Matt Dowle
  • 57,542
  • 22
  • 163
  • 221
mnel
  • 110,110
  • 27
  • 254
  • 248
1

More generally, for two conditions:

RDF <- DF[ DF$cat == "A" | DF$dog == "B", ]
Ryan C. Thompson
  • 38,976
  • 28
  • 92
  • 152
  • 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