產生並比較配對

R 的資料清理

Maggie Matsui

Content Developer @ DataCamp

無法用連接時

左表含 event 與 time 欄。比賽:Houston Rockets 對 Chicago Bulls,19:00。Miami Heat 對 Los Angeles Lakers,19:00。Brooklyn Nets 對 Orlando Magic,20:00。Denver Nuggets 對 Miami Heat,21:00。San Antonio Spurs 對 Atlanta Hawks,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 的資料清理

無法用連接時

R 的資料清理

什麼是紀錄連結?

record_linkage_1.png

R 的資料清理

什麼是紀錄連結?

左側兩個拿著掃把的資料庫,標示為 Data A 與 Data B

R 的資料清理

什麼是紀錄連結?

從 Data A 與 Data B 指向三組人物的箭頭,標示為 Generate pairs

R 的資料清理

什麼是紀錄連結?

箭頭從 generate pairs 指向兩欄圓點,彼此以不同方向箭頭相互指向。說明為 compare pairs。

R 的資料清理

什麼是紀錄連結?

箭頭由 compare pairs 指向一個人舉著 .93 的牌子。標示為 score pairs。

R 的資料清理

什麼是紀錄連結?

從 score pairs 指向一條鍊子的箭頭,標示為 link data。

R 的資料清理

什麼是紀錄連結?

相同流程圖,藍框標出 generate pairs 步驟

R 的資料清理

紀錄配對

兩個資料表 df_A 與 df_B,包含姓名、郵遞區號與州別。df_A 的 Keaton Z Snyder(zip 15020,州 PA)與 df_B 的 Keaton Snyder(zip 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)

相同資料表,但只連接州別相同的列。

僅在封鎖變數(State)相同時才考慮成對

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 的資料清理

比較配對

紀錄連結流程圖,突出顯示 compare pairs 步驟。

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 的資料清理

一起來練習吧!

R 的資料清理

Preparing Video For Download...