我对R编程非常陌生。我正在处理一些数据。这些数据是每天从一群人中收集的。通常,数据的格式是。
姓名,出生年月日,出生地,年龄,LGA
以文本格式填充字符串向量。
text <- c()
在这里 高频 链接到一个数据库,为每个 LGA共10个)。也就是说,每一个LGA都是一组高频产品
有趣的是,由于对格式的遵守程度较低,高频词的拼写通常有很多错误。
以下是数据的样本
"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代码通过一些单词匹配语法来提取LGAs(因为它们很少),涵盖了通常看到的可能的拼写错误。
#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为例,其标准拼写下的HF约有200个。
我基本上是在尝试着填充向量。
Hf <- c()
与LGA有关的HF的适当单词拼写。
有没有一种语法可以说。
对于文本中找到的每个LGA组,扫描是否有HF(在LGA组中)匹配。如果匹配,则填充向量Hf。
谁能帮帮我。谅谅
好吧,我想你首先要解决一个初始问题。 你的数据结构如果有错位的逗号,必然会造成问题。我会先解决这个问题,仔细分解这些字符串,确定问题输入,像这样......
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))
创建于2020-05-13 重读包 (v0.3.0)