【问题标题】:String scan and match with respect to group in R字符串扫描和匹配R中的组
【发布时间】:2020-08-29 16:32:03
【问题描述】:

我对 R 编程非常陌生。我正在处理一些数据。每天从一群人那里收集数据。通常,数据的格式是:

姓名、出生日期、HF、LGA

以文本格式填充字符串向量

 text <- c()

这里,HF 链接到每个 LGA(总共 10 个)的数据库。也就是说,每个 LGA 都是一组 HF

有趣的是,由于对格式的遵从程度低,在 HF 的拼写中通常存在很多错误。

这是数据样本

 "first person Usman,03May2019,Ntade Health post,LGA1"
 "second person, 7may2019,phc,makirin, LGA2"

#Here, "phc,makirin" is supposed to be spelt "Phc Makirine"

我已经能够通过一些单词匹配语法使用 R 代码提取 LGA(因为它们很少),涵盖通常看到的拼写错误

#LGA vector
library(stringr)
LGA <- c()
LGA[str_detect(text_from_optin, regex("Alier|Aleiro|Alero", ignore_case = TRUE))] <- "ALIERO"
LGA[str_detect(text_from_optin, regex("Augie|Agie|Auge|Auggie?", ignore_case = TRUE))] <- "AUGIE"
LGA[str_detect(text_from_optin, regex("Bagudo", ignore_case = TRUE))] <- "BAGUDO"
LGA[str_detect(text_from_optin, regex("Bir?nin Kebb?i|BirninKebn?i|B\\Kebb?i|Binin|birninkebbi", ignore_case = TRUE))] <- "BIRNIN KEBBI"
LGA[str_detect(text_from_optin, regex("Dan?di", ignore_case = TRUE))] <- "DANDI"
LGA[str_detect(text_from_optin, regex("Danko?wasa|Wasagu|D\\Was|Dankowasagu|Danko", ignore_case = TRUE))] <- "DANKO WASAGU"
LGA[str_detect(text_from_optin, regex("Fakai", ignore_case = TRUE))] <- "FAKAI"
LGA[str_detect(text_from_optin, regex("Gw?andu", ignore_case = TRUE))] <- "GWANDU"
LGA[str_detect(text_from_optin, regex("Kalg", ignore_case = TRUE))] <- "KALGO"
LGA[str_detect(text_from_optin, regex("Koko Bes|K\\Bes|Kokobess?", ignore_case = TRUE))] <- "KOKO BESSE"

对于 LGA,例如 Aliero,在其标准拼写下大约有 200 个 HF

我基本上是在尝试填充向量

Hf <- c()

相对于 LGA 使用适当的 HF 单词拼写

有没有语法可以说:

对于文本中找到的每个 LGA 组,扫描是否有任何 HF(在 LGA 组中)匹配。如果匹配,则填充向量 Hf

谁能帮帮我。谢谢

【问题讨论】:

  • 试图通过模式匹配来“修复”失效的源数据库将总是失败。无论您做什么,都会出现一个新的拼写错误并且不会被识别。同样危险:会出现一个新的有效名称,但您已经将其重新映射为其他名称。除了 ignore_case 之外,还有人必须修复数据库

标签: r string dataframe vector


【解决方案1】:

嗯,我认为您有一个需要首先解决的初始问题。如果逗号放错了地方,您的数据结构必然会导致问题。我会首先通过仔细分解这些字符串并识别这样的问题输入来解决这个问题......

library(dplyr)
library(tidyr)

yourdata <- read.csv("your textfile", header = FALSE)

yourdata
#>                                                    V1
#> 1 first person Usman,03May2019,Ntade Health post,LGA1
#> 2           second person, 7may2019,phc,makirin, LGA2

newyourdata <- tidyr::separate(data = yourdata, 
                               col = V1, 
                               sep = ",", 
                               into = c("name", "DOB", "HF", "LGA", "problem"), 
                               remove = FALSE, 
                               extra = "merge", 
                               fill = "right")

newyourdata %>% filter(!is.na(problem))

#>                                          V1          name       DOB  HF     LGA
#> 1 second person, 7may2019,phc,makirin, LGA2 second person  7may2019 phc makirin
#>   problem
#> 1    LGA2

unique(newyourdata$HF)
#> [1] "Ntade Health post" "phc"

可重复的数据

yourdata <- structure(list(V1 = c("first person Usman,03May2019,Ntade Health post,LGA1", 
                                  "second person, 7may2019,phc,makirin, LGA2")), class = "data.frame", row.names = c(NA, 
                                                                                                                     -2L))

reprex package (v0.3.0) 于 2020-05-13 创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    相关资源
    最近更新 更多