0

I am trying to make a vector of zip codes to be used as in dplyr for filtering a dataset. The dataset has multiple variables, but for my question I just showed the zip_code Variable.

head(df)
     df
1 75251
2 75219
3 76051
4 75209
5 75224
6 76006

school_zips_V <- c(
"75244",
"75211",
"75134",
"75038",
"75150",
"75243")

I tried using Str_Detect from code i've researched online but get errors.

Clean_Data <- filter(df, str_detect(school_zips_V, paste(school_zips_V)))

Error in filter_impl(.data, quo) : Result must have length 4429, not 21

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Koolakuf_DR
  • 467
  • 4
  • 16

2 Answers2

2

You'd use %in%, no need for str_detect:

df %>%
  filter(zip_code %in% school_zips_V)
arg0naut91
  • 14,574
  • 2
  • 17
  • 38
2

Try %in%:

library(dplyr)
school_zips_V <- c(
  "75244",
  "75211",
  "75134",
  "75038",
  "75150",
  "75243")
df <- tibble(zip = c("75251", "75244", "90210"))
filter(df, zip %in% school_zips_V)
#> # A tibble: 1 x 1
#>   zip  
#>   <chr>
#> 1 75244

Created on 2019-02-23 by the reprex package (v0.2.1)

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Øystein S
  • 536
  • 2
  • 11