我正在尝试创建一个邮编的向量,以在dplyr中用作过滤数据集。数据集具有多个变量,但是对于我的问题,我只是显示了zip_code变量。
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")
我尝试从网上研究的代码中使用Str_Detect,但出现错误。
Clean_Data <- filter(df, str_detect(school_zips_V, paste(school_zips_V)))
filter_impl(.data,quo)中的错误:结果的长度必须为4429,而不是21
答案 0 :(得分:2)
您将使用%in%
,不需要str_detect
:
df %>%
filter(zip_code %in% school_zips_V)
答案 1 :(得分:2)
尝试%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
由reprex package(v0.2.1)于2019-02-23创建