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
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
You can use data.table and keys
library(data.table)
DT <- data.table(DF)
setkey(DT, cat)
DT[c("A", "B"),]
More generally, for two conditions:
RDF <- DF[ DF$cat == "A" | DF$dog == "B", ]