我有兩個非常大的資料框,其中包含人名。這兩個資料框報告了關于這些人的不同資訊(即 df1 報告有關健康狀況的資料,而 df2 報告有關社會經濟狀況的資料)。一部分人出現在兩個資料框中。這是我感興趣的示例。我需要創建一個新的資料框,其中僅包含出現在兩個資料集中的人。但是,名稱有細微差別,主要是由于拼寫錯誤。
我的資料如下:
df1
name | smoker | age
"Joe Smith" | Yes | 43
"Michael Fagin" | Yes | 35
"Ellen McFarlan" | No | 55
...
...
df2
name | occupation | location
"Joe Smit" | Postdoc | London
"Joan Evans" | IT consultant | Bristol
"Michael Fegin" | Lawyer | Liverpool
...
...
我需要的是具有以下資訊的第三個資料幀 df3:
df3
name1 | name2 | distance | smoker | age | occupation | location
"Joe Smith" | "Joe Smit" | a measure of their Jaro distance | Yes | 43 | Postdoc | London
"Michael Fagin" | "Michael Fegin" | a measure of their Jaro distance | Yes | 35 | Lawyer | Liverpool
...
...
到目前為止,我已經使用 stringdist 包來獲取可能匹配的向量,但我正在努力使用這些資訊來創建一個包含我需要的資訊的新資料框。如果有人對此有任何想法,請提前非常感謝!
uj5u.com熱心網友回復:
library(tidyverse)
library(fuzzyjoin)
df1 <- tibble(
name = c("Joe Smith", "Michael Fagin"),
smoker = c("yes", "yes")
)
df2 <- tibble(
name = c("Joe Smit", "Michael Fegin"),
occupation = c("post doc", "IT consultant")
)
df1 %>%
# max 3 chars different
stringdist_inner_join(df2, max_dist = 3)
#> Joining by: "name"
#> # A tibble: 2 × 4
#> name.x smoker name.y occupation
#> <chr> <chr> <chr> <chr>
#> 1 Joe Smith yes Joe Smit post doc
#> 2 Michael Fagin yes Michael Fegin IT consultant
由reprex 包于 2022-03-01 創建 (v2.0.0 )
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435682.html
