我愿意在python pandas中这样做,但在R中我有以下df:
result<-structure(list(traffic_Count_Street = c("San Angelo", "W Commerce St",
"W Commerce St", "S Gevers St", "Austin Hwy", "W Evergreen St"
), unit_Street = c("San Pedro Ave", "W Commerce", "W Commerce",
"S New Braunfels", "Austin Highway", "W Cypress")), .Names = c("traffic_Count_Street",
"unit_Street"), row.names = c(1L, 17L, 18L, 34L, 260L, 273L), class = "data.frame")
1 San Angelo San Pedro Ave
17 W Commerce St W Commerce
18 W Commerce St W Commerce
34 S Gevers St S New Braunfels
260 Austin Hwy Austin Highway
273 W Evergreen St W Cypress
对于每一行,如果其中一个字符(大于3个字符)与另一个字符匹配,我希望将第1列与第2列部分匹配。
我会删除:
1 San Angelo San Pedro Ave
34 S Gevers St S New Braunfels
273 W Evergreen St W Cypress
并保持:
17 W Commerce St W Commerce
18 W Commerce St W Commerce
260 Austin Hwy Austin Highway
我尝试以下列方式使用stringR
,但它不起作用:
result$unit_Street[str_detect(result$traffic_Count_Street, "\\w{3}")]
答案 0 :(得分:1)
创建具有阈值调整的距离过滤器。然后你可以调整,直到你得到你想要的结果。在这种情况下,Levenshtein距离为5表现良好:
distanceFilter <- function(df, thresh=5) {
ind <- apply(df, 1, function(x) adist(x[1], x[2]) < thresh )
df[ind,]
}
distanceFilter(result, 5)
# traffic_Count_Street unit_Street
# 17 W Commerce St W Commerce
# 18 W Commerce St W Commerce
# 260 Austin Hwy Austin Highway
要了解详情,请参阅the wiki page和R doc help page