R:通过拼写错误的字符列连接两个数据帧

时间:2016-11-14 00:15:37

标签: r dataframe dplyr tidyverse

我的两个数据框具有相同的字符列。使用dplyr :: full_joint通过此列加入它们很容易。但问题是共同列在拼写上有轻微但明显的差异。相对于定义技能的每个字符串,拼写差异很小:

Skill                   Grade_Judge_A

pack & ship               1
pack & store              5
sell                      3
Design a room             9


Skill                   Grade_Judge_B

pack and store            3
pack & ship               7
sell                      2
Design room               6

如何在下面获得所需的输出:

Skill                   Grade_Judge_A      Grade_Judge_B

pack & ship               1                     3                
pack & store              5                     7
sell                      3                     2
Design a room             9                     6

我在考虑根据“技能”列中字符串之间的距离匹配两个数据帧中的行,例如使用stringdist包。如果两个字符串之间的差异很小,那么这意味着技能是相同的。

我更喜欢dplyr / tidyverse解决方案。

这是数据帧A的实际输入:

> dput(df_A)

structure(list(skill = c(" [Assess abdomen for a floating mass]", 
" [Assess Nerve Root Compression]", " [Evaluate breathing effort (rate, patterns, chest expansions)]", 
" [Evaluate Plantar Reflex/Babinski sign]", " [Evaluate Speech]", 
" [External palpation of a uterus]", " [Heel to Shin test]", 
" [Inspect anterior chamber of eye with ophthalmoscope or penlight]", 
" [Inspect breast]", " [Inspect Overall Skin Color/Tone]", " [Inspect Skin Lesions]", 
" [Inspect Wounds]", " [Mental Status/level of consciousness]", 
" [Nose/index finger]", " [Percuss abdomen to determine spleen size]", 
" [Percuss costovertebral angle for kidney tenderness]", " [Percuss for diaphragmatic excursion]", 
" [Percuss the abdomen for abdominal tones]", " [Percuss the abdomen to determine liver span]"
), `2016-09-17 13:41:08` = c(1, 1, 5, 3, 4, 0, 4, 3, 3, 5, 4, 
5, 5, 3, 1, 1, 2, 4, 1)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -19L), .Names = c("skill", "2016-09-17 13:41:08"
))

和dataframe B:

> dput(df_B)

structure(list(skill = c(" [Assess abdomen for floating mass]", 
" [Assess nerve root compression]", " [Evaluate breathing effort (rate, patterns, chest expansion)]", 
" [Evaluate plantar reflex/Babinski sign]", " [Evaluate speech]", 
" [External palpation of uterus]", " [Heel to shin test]", " [Inspect anterior chamber of the eye with opthalmoscope or penlight]", 
" [Inspect breasts]", " [Inspect overall skin color/tone]", " [Inspect skin lesions]", 
" [Inspect wounds]", " [Mental status/level of consciousness]", 
" [Nose/Index finger]", " [Percuss costovertebral angle for kidney tenderness]", 
" [Percuss for diaphragmatic excursion]", " [Percuss the abdomen for abdominal tones]", 
" [Percuss the abdomen to determine liver span]", " [Percuss the abdomen to determine spleen size]"
), `2016-09-21 07:58:43` = c(0, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -19L), .Names = c("skill", "2016-09-21 07:58:43"
))

以下是两个数据框的负责人:

 > head(df_A)
        # A tibble: 6 × 2
                                                                    skill `2016-09-17 13:41:08`
                                                                    <chr>                 <dbl>
        1                            [Assess abdomen for a floating mass]                     1
        2                                 [Assess Nerve Root Compression]                     1
        3  [Evaluate breathing effort (rate, patterns, chest expansions)]                     5
        4                         [Evaluate Plantar Reflex/Babinski sign]                     3
        5                                               [Evaluate Speech]                     4
        6                                [External palpation of a uterus]                     0

和第二个:

> head(df_B)
# A tibble: 6 × 2
                                                           skill `2016-09-21 07:58:43`
                                                           <chr>                 <dbl>
1                             [Assess abdomen for floating mass]                     0
2                                [Assess nerve root compression]                     2
3  [Evaluate breathing effort (rate, patterns, chest expansion)]                     2
4                        [Evaluate plantar reflex/Babinski sign]                     2
5                                              [Evaluate speech]                     2
6                                 [External palpation of uterus]                     1

2 个答案:

答案 0 :(得分:0)

如果拼写错误中没有模式,我相信唯一剩下的方法是在加入数据之前确保拼写相同。我们可以用 splitstackshape

library(splitstackshape)

yourdata$skill<-stri_replace_all(yourdata8$skill,"pack & store" ,fixed = "pack and store")

此代码将pack and store替换为数据集pack & store列中的skill

答案 1 :(得分:0)

这有多接近?

require(tidyverse)
require(stringdist)

df_A %>%
    rownames_to_column %>%
    mutate(foo=1) %>%
    full_join((df_B %>% rownames_to_column %>% mutate(foo=1)), by='foo') %>%
    select(-foo) %>%
    mutate(dist = stringdist(skill.x, skill.y), norm_dist = dist / length(skill.x)) %>%
    arrange(norm_dist) %>%
    filter(norm_dist < 0.015)

我在df_Adf_B上进行了真正的(关系代数式)完全加入,如果您拥有的实际数据很大(例如,如果两个数据框都有1000行,连接的结果将是1,000,000行)。这种连接是通过创建一个虚拟列foo完成的,该列对于每一行都是相等的,然后连接到虚拟列。

注释中提到的stringdist包然后将两个字符串的每个可能组合的行A与行B进行比较。对于您的示例数据,标准化字符串距离的截止值为0.015,结果似乎很好。当然,这个任意截止点可能会超出您的示例数据。