生成并比较配对

R 中的数据清洗

Maggie Matsui

Content Developer @ DataCamp

当连接(join)不适用时

左表含事件与时间:休斯顿火箭 vs 芝加哥公牛 19:00;迈阿密热火 vs 洛杉矶湖人 19:00;布鲁克林篮网 vs 奥兰多魔术 20:00;丹佛掘金 vs 迈阿密热火 21:00;圣安东尼奥马刺 vs 亚特兰大老鹰 21:00。右表含:NBA: Nets vs Magic 8pm;NBA: Bulls vs Rockets 9pm;NBA: Heat vs Lakers 7pm;NBA: Grizzlies vs Heat 10pm;NBA: Heat vs Cavaliers 9pm。

R 中的数据清洗

当连接(join)不适用时

R 中的数据清洗

什么是记录链接?

record_linkage_1.png

R 中的数据清洗

什么是记录链接?

左侧为两份带扫帚的数据库,标为 Data A 与 Data B

R 中的数据清洗

什么是记录链接?

从 Data A 和 Data B 指向三对人物的箭头,标注:生成配对。

R 中的数据清洗

什么是记录链接?

箭头从"生成配对"指向两列相互指向的圆点。说明为:比较配对。

R 中的数据清洗

什么是记录链接?

箭头从"比较配对"指向一位举着".93"牌子的人。标注:为配对打分。

R 中的数据清洗

什么是记录链接?

箭头从"为配对打分"指向一条链,标注:链接数据。

R 中的数据清洗

什么是记录链接?

同一图示,蓝框标出"生成配对"步骤

R 中的数据清洗

记录配对

两张表 df_A 与 df_B,包含姓名、邮编、州。df_A 中 Keaton Z Snyder(15020,PA)高亮;df_B 中 Keaton Snyder(15020,PA)高亮。

R 中的数据清洗

生成配对

同样两表,从 df_A 每行连到 df_B 每行,表示所有组合。

R 中的数据清洗

在 R 中生成配对

library(reclin)
pair_blocking(df_A, df_B)
Simple blocking
  No blocking used.
  First data set:  5 records
  Second data set: 5 records
  Total number of pairs: 25 pairs

ldat with 25 rows and 2 columns x y 1 1 1 2 2 1 3 3 1 ...
R 中的数据清洗

配对过多

同样两表向下扩展更多行,连接线大幅增加。

R 中的数据清洗

封锁(Blocking)

同样两表,但只连"州"相同的行。

仅在封锁变量(州)一致时考虑配对

R 中的数据清洗

R 中的配对封锁

pair_blocking(df_A, df_B, blocking_var = "state")
Simple blocking                                 ldat with 8 rows and 2 columns
  Blocking variable(s): state                     x y
  First data set:  5 records                    1 1 1
  Second data set: 5 records                    2 1 4
  Total number of pairs: 8 pairs                3 2 3
                                                4 2 5
                                                5 3 2
                                                6 4 2
                                                7 5 1
                                                8 5 4
R 中的数据清洗

比较配对

记录链接流程图,突出显示"比较配对"步骤。

R 中的数据清洗

比较配对

pair_blocking(df_A, df_B, blocking_var = "state") %>%

compare_pairs(by = "name", default_comparator = lcs())
Compare                                     ldat with 8 rows and 3 columns            
  By: name                                      x y      name
                                              1 1 1 0.3529412
Simple blocking                               2 1 4 0.3030303
  Blocking variable(s): state                 3 2 3 0.9285714
  First data set:  5 records                  4 2 5 0.2962963    
  Second data set: 5 records                  ...
  Total number of pairs: 8 pairs              8 5 4 0.3333333
R 中的数据清洗

比较多列

pair_blocking(df_A, df_B, blocking_var = "state") %>%
  compare_pairs(by = c("name", "zip"), default_comparator = lcs())
Compare                                    ldat with 8 rows and 4 columns
  By: name, zip                              x y      name zip
                                           1 1 1 0.3529412 0.4
Simple blocking                            2 1 4 0.3030303 0.2
  Blocking variable(s): state              3 2 3 0.9285714 1.0
  First data set:  5 records               4 2 5 0.2962963 0.2
  Second data set: 5 records               ...
  Total number of pairs: 8 pairs           8 5 4 0.3333333 0.2
R 中的数据清洗

不同的比较器

  • default_comparator = lcs()
  • default_comparator = jaccard()
  • default_comparator = jaro_winkler()
R 中的数据清洗

Passons à la pratique !

R 中的数据清洗

Preparing Video For Download...