如何使用 r 查找邮政编码中的两个三

问题描述 投票:0回答:1

我需要帮助完成此任务: 打印 address.zip 代码中包含两个三的位置的数据。 我试过:

filtered_data <- df %>%
  filter(grepl("\\d{3}.*\\d{3}", address.zip))
  print(filtered_data)

它没有给出任何输出,但我知道数据文件中有带有两个三的邮政编码

r data-manipulation data-processing
1个回答
0
投票
library(stringr)
d <- data.frame(
  zipcode = c("00001", "10003", "20003", "30003"
), 
  addr = c("123 Main", "456 Maple", "789 Mulberry", "12302 Arrow"
))
d[which(str_count(d$zipcode,"3") == 2),"addr"]
#> [1] "12302 Arrow"

创建于 2023-09-19,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.