0

I have some dataframe and I want to select the rows according to certain factors. What I can do now is that build sub-dataframe respectively, then combine them together. Is that another efficient way to do that?

For instance, I want to pick the rows which in a1 is "F" and the rows which a4 is "John" together.
a1 <- c("F","M","F","M","F","F")
a2 <- c("S","S","S","T","T","S")
a3 <- c(250, 66, 95, 2, 1, 4)
a4 <- c("John","Annie","King","John","Tom","Lily")
df <- data.frame(a1,a2,a3,a4)
df2 <- df[df$a1 == "F",]   # sub-dataframe which a1 is "F"
df3 <- df[df$a4 == "John",]  # sub-dataframe which a4 is "John"
rbind(df2,df3) # combine them together
#ideal result
> rbind(df2,df3)
a1 a2  a3   a4
1   F  S 250 John
3   F  S  95 King
5   F  T   1  Tom
6   F  S   4 Lily
11  F  S 250 John
4   M  T   2 John
Codezy
  • 662
  • 5
  • 17

1 Answers1

0

Using base R you can achieve this by inserting the 'OR' operator

df[df$a1 == "F" | df$a4 == "John",]

Another way of doing this is with the dplyr package:

df %>% filter(a1 == "F" | a4 == "John")
Philipp HB
  • 169
  • 1
  • 14