根据 R 中的字符串距离匹配两列

发布于 2025-01-10 20:34:11 字数 898 浏览 0 评论 0原文

我有两个非常大的数据框,其中包含人名。这两个数据框报告了这些人的不同信息(即 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 包来获取可能匹配的向量,但我正在努力使用这些信息来创建一个包含我需要的信息的新数据框。如果有人对此有想法,请提前非常感谢!

I have two very large dataframes containing names of people. The two dataframes report different information on these people (i.e. df1 reports data on health status and df2 on socio-economic status). A subset of people appears in both dataframes. This is the sample I am interested in.
I would need to create a new dataframe which includes only those people appearing in both datasets. There are, however, small differences in the names, mostly due to typos.

My data is as follows:

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
...
...

What I would need is to have a third dataframe df3 with the following information:

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
...
...

So far I have worked with the stringdist package to get a vector of possible matches, but I am struggling to use this information to create a new dataframe with the information I need. Many thanks in advance should anyone have an idea for this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2025-01-17 20:34:11
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 软件包 (v2.0.0) 创建于 2022 年 3 月 1 日

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

Created on 2022-03-01 by the reprex package (v2.0.0)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文