比較字串

R 的資料清理

Maggie Matsui

Content Developer @ DataCamp

測量數值間的距離

一條從 0 到 15 的數線,分別在 3 與 10 上有一個點。

R 的資料清理

測量數值間的距離

同一條數線,紅色括號標示 3 與 10 之間的距離。

R 的資料清理

測量數值間的距離

在紅線下方顯示距離,10 減 3 等於 7。

                                            「typhoon」和「baboon」之間的距離是多少?

R 的資料清理

最小編輯距離

要把一個字串變成另一個字串,需要幾個拼寫錯誤?

代表插入的綠色加號。

R 的資料清理

最小編輯距離

要把一個字串變成另一個字串,需要幾個拼寫錯誤?

代表刪除的紅色減號

R 的資料清理

最小編輯距離

要把一個字串變成另一個字串,需要幾個拼寫錯誤?

代表取代的紫色循環符號。

R 的資料清理

最小編輯距離

要把一個字串變成另一個字串,需要幾個拼寫錯誤?

代表換位的藍色雙箭頭。

R 的資料清理

編輯距離 = 1

以綠色加號顯示 dog 與 dogs 的差異。

R 的資料清理

編輯距離 = 1

以紅色減號顯示 bath 與 bat 的差異。

R 的資料清理

編輯距離 = 1

以紫色循環符號顯示 cats 與 rats 的差異。

R 的資料清理

編輯距離 = 1

以藍色雙箭頭顯示 sign 與 sign 的差異。

R 的資料清理

更複雜的例子

baboon $\rightarrow$ typhoon

字詞 baboon

R 的資料清理

更複雜的例子

baboon $\rightarrow$ typhoon
  • 插入 h

在 baboon 下方,顯示 babhoon,h 上方有綠色加號。

R 的資料清理

更複雜的例子

baboon $\rightarrow$ typhoon
  • 插入 h
  • b $\rightarrow$ t

在其下方顯示 tabhoon,t 上方有紫色循環符號

R 的資料清理

更複雜的例子

baboon $\rightarrow$ typhoon
  • 插入 h
  • b $\rightarrow$ t
  • a $\rightarrow$ y

在其下方顯示 tybhoon,y 上方有紫色循環符號。

R 的資料清理

更複雜的例子

baboon $\rightarrow$ typhoon
  • 插入 h
  • b $\rightarrow$ t
  • a $\rightarrow$ y
  • b $\rightarrow$ p

總計:4

在其下方顯示 typhoon,p 上方有紫色循環符號。

R 的資料清理

編輯距離的種類

  • Damerau-Levenshtein
    • 你剛學到的方法
  • Levenshtein
    • 只考慮取代、插入、刪除
  • LCS (Longest Common Subsequence)
    • 只考慮插入與刪除
  • 其他
    • Jaro-Winkler
    • Jaccard

哪個最好?

R 的資料清理

R 中的字串距離

library(stringdist)
stringdist("baboon", 
           "typhoon",
           method = "dl")
4

沿用前面投影片,示範如何從 baboon 變到 typhoon。

R 的資料清理

其他方法

# LCS
stringdist("baboon", "typhoon",
           method = "lcs")
7
# Jaccard
stringdist("baboon", "typhoon",
           method = "jaccard")
0.75
R 的資料清理

用比較字串來清理資料

  • 第 2 章中:
    • "EU""eur""Europ" $\rightarrow$ "Europe"
  • 若變體太多怎麼辦?
    • "EU""eur""Europ""Europa""Erope""Evropa"、… $\rightarrow$ "Europe"
    • 用字串距離!
R 的資料清理

用比較字串來清理資料

survey
          city move_score
1       chicgo          4
2   los angles          4
3      chicogo          5
4      new yrk          5
5    new yoork          2
6     seatttle          3
7   losangeles          4
8      seeatle          2
...
cities
         city
1    new york
2     chicago
3 los angeles
4     seattle
R 的資料清理

用字串距離重新對映

library(fuzzyjoin)
stringdist_left_join(survey, cities, by = "city", method = "dl")
        city.x move_score      city.y
1       chicgo          4     chicago
2   los angles          4 los angeles
3      chicogo          5     chicago
4      new yrk          5    new york
5    new yoork          2    new york
6     seatttle          3     seattle
7   losangeles          4 los angeles
8      seeatle          2     seattle
9      siattle          1     seattle
...
R 的資料清理

用字串距離重新對映

stringdist_left_join(survey, cities, by = "city", method = "dl", max_dist = 1)
        city.x move_score      city.y
1       chicgo          4     chicago
2   los angles          4 los angeles
3      chicogo          5     chicago
4      new yrk          5    new york
5    new yoork          2    new york
6     seatttle          3     seattle
7   losangeles          4 los angeles
8      seeatle          2        <NA>
9      siattle          1     seattle
...
R 的資料清理

一起來練習吧!

R 的資料清理

Preparing Video For Download...