1

Suppose I have a dataframe that I want to filter out certain elements, in this case I want to only filter out specific colleges from the dataframe. The column I am filtering by is called "OWNER" and I want to filter out several schools such as "DUKE", "COLUMBIA", "STANFORD" etc.

If I make an vector of the names of schools, how can I use the vector to filter out the specific schools?

1 Answers1

2

We can use subset with %in%

v1 <- c("DUKE", "COLUMBIA", "STANFORD")
df2 <- subset(df1, OWNER %in% v1 )

If we need to remove those schools, negate (!)

df2 <- subset(df1, !OWNER %in% v1 )

With dplyr, we can use filter

library(dplyr)
df2 <- df1 %>%
          filter(OWNER %in% v1)
akrun
  • 874,273
  • 37
  • 540
  • 662