在R中使用模糊匹配合并列上的两个数据帧
我有两个数据帧,一个用于2008年,另一个用于2004年和2012年的数据。下面是数据帧的示例在R中使用模糊匹配合并列上的两个数据帧,r,merge,R,Merge,我有两个数据帧,一个用于2008年,另一个用于2004年和2012年的数据。下面是数据帧的示例 df_08 <- read.table(text = c(" observation year x code_location location 1 2008 300 23-940 town no. 1 town no. 1 2 2008 234 23-941 town no.
df_08 <- read.table(text = c("
observation year x code_location location
1 2008 300 23-940 town no. 1 town no. 1
2 2008 234 23-941 town no. 2 town no. 2
3 2008 947 23-942 city St 23 city St 23
4 2008 102 23-943 Mtn town 5 Mtn town 5 "), header = TRUE)
df_04_12 <- read.table(text = c("
observation year y code_location location
1 2004 124 23-940 town no. 1 town no. 1
2 2004 395 23-345 town # 2 town # 2
3 2004 1349 23-942 city St 23 city St 23
4 2012 930 53-443 Mtn town 5 Mtn town 5
5 2012 185 99-999 town no. 1 town no. 1
6 2012 500 23-941 town Number 2 town Number 2
7 2012 185 34-942 city Street 23 city Street 23
8 2012 195 23-943 Mt town 5 Mt town 5 "), header = TRUE)
可以在字符变量上使用levenshtein距离,但无法解释符号。我建议您在合并之前清除所有符号,然后使用stringdist包。对于这个问题没有干净的解决方案,您必须开发自己的方法,因为它与您的数据相关。 模糊匹配中使用的一些方法是字符串距离计算和数据的SoundX转换,您只需找出适合您的数据的方法。查看fuzzyjoin软件包。
observation year y code_location location.x location.y y.y
1 2004 124 23-940 town no. 1 town no. 1 town no.1 300
2 2004 395 "23-345 town # 2" "town # 2" "town no. 2" 234
3 2004 1349 23-942 city St 23 city St 23 city St 23 947
4 2012 930 53-443 Mtn town 5 Mtn town 5 Mtn town 5 102
5 2012 185 99-999 town no. 1 town no. 1 town no. 1 300
6 2012 500 23-941 town Number 2 town Number 2 town no. 2 234
7 2012 185 34-942 city Street 23 city Street 23 city St 23 947
8 2012 195 23-943 Mt town 5 Mt town 5 Mtn town 5 102