如何解决是否有一个函数可以查看变量中的单词如何出现在另一个变量中以及 R 中的数据集?
我需要在 R 中查找代码的帮助:我有一个由不同变量组成的文件 excel,但我需要比较两个变量(在我的情况下都是人名),以查看第一个变量中的名称(列)出现在第二个变量(列)中的次数。是非常大的数据集,有很多行。 提前谢谢你!!
数据如下,
df <- structure(
list(
Inv_name = c(
"Meyer,Helmut-Walter,Prof. Dr. sc.","Fröhlich,Hans-Otto,Prof. Dr.","Göbel,Andreas","Klein,Wilfried","Scholz,Peter,Dr.","Hipler,Bernd","Sammler,Marina","Kreisel,Günter,"Haupt,Kerstin","Bayer,Ulrich","Schmidt,Jürgen","Furche,Thomas","Thorwirth,Günter","Kowalski,Peter","Geiler,Hans-Dieter,"Wagner,Matthias,"RIESENBERG,Dieter","SCHULZ,Volker","KNORRE,Wofgang","POHL,Hans,Dieter"
),Prio_Year = c(
1990,1990,1990
),Firma = c(
"Gustav Peters","id - Informationsdienst,Geschäftsvermittlung,Handelsvertretung,Marketing Kursawe & Klein OHG","\"Heizhaus-Service\" Ingenieurbüro Wilfried Gärtner","MAX LINDAU OHG Inhaber: Gerhard und Klaus-Dieter Lindau","Werther Götze \"Erfordia Numismatica\"","STUDIO 1 - John & Ziemann","Arwico-Arenswald & Wichmann OHG","Moschkowitz & Co. Schweißtechnik-Vertriebs KG","Fahrschule Simon & Frauendorf","Elmera Vertrieb Frank Fleischmann OHG","OHG Friseur Walschleben","Baumarkt Hartmut Seliger","\"Thüringer MarketingService - Hoffmann & Co. OHG\"","Walter Reuning Fleisch-Wurstwaren Inhaber Brigitte Pfautsch","Thüringer Allgemeine Verlag GmbH & Co,KG","Autoservice Grohmann-Lämmerhirt OHG","\"Sippel und Thomas Video - Land\" Offene Handelsgesellschaft","Paul Krause","Alois Reuter - Stapler-Instandsetzung","Rudolf Städtler Inhaber Willy Städtler"
),Ersteintrag = structure(
c(
-2209075200,643420800,643248000,645235200,646617600,646963200,648432000,649382400,648777600,648950400,649468800,650073600,650678400,-2156112000,650678400
),tzone = "UTC",class = c("POSIXct","POSIXt")
)
),row.names = c(NA,-20L),class = c("tbl_df","tbl","data.frame")
)
目的是将 Inv_name
中的元素依次与 Firma
中的所有索引进行比较,并计算部分匹配的数量,如果 df$Inv_name[[1]] = "A"
则计数将为 {{1 }},如果2
。
for (i in 1:nrow(df1)){
-
df$Firma = c("A GMBH","B GMBH","A Inc")
-
# Extract the Inv_name
-
# as a matrix; this will be our pattern search
-
# along the Firma
-
tmpName <- str_to_lower(
-
str_split(df1$Inv_name[i],pattern = ",",simplify = T)
-
)
-
# Create a new variable that counts the number of
-
# occurences of the id
-
str_detect(
-
string = str_to_lower(df1$Firma),
-
pattern = paste(tmpName,sep = "",collapse = "|")
-
)
-
# NOTE: That it it cannot count correctly
-
# due to titles like Dr. Mr. and MS.
-
# If you are certaing about the pattern on
-
# how these titles are generated then we can ignore these.
-
# Here we remove all matches on the form Dr. Mr. Ms.
-
tmpName <- str_remove_all(
-
tmpName,
-
pattern = "[:alpha:]+[:punct:][:blank:]+"
-
)
-
# We then recount without titles
-
# to avoid confusion
-
df1$count2[i] <- sum(
-
str_detect(
-
string = str_to_lower(df1$Firma),collapse = "|")
-
)
- }
警告信息:
1:未知或未初始化的列:
)
。 2:未知或未初始化的列:count1
。
tmp 名称 [1] 不适用
解决方法
您要查找的函数是 str_detect()
包中的 stringr
。
这可能是我会做的。请注意,此解决方案假设您有一个数据帧 df
,其索引少于 500k。如果超过此数量,则提供的算法可能需要太多时间才能运行。但是测试一下,看看效果如何。
如果你没有安装相关的包,运行下面这行代码,
install.packages("tidyverse")
library("tidyverse")
此外,我生成了一个通用的 df
来指出执行 string-matching
时的潜在缺陷。
数据
df <- data.frame(
id = c("Dr. A,D"," Mr. B,C","Ms. E,F,D"),string = c("ABC Bankrupt","ADEF LTD","D GMBH")
)
这里id
对应Inv_name
,string
对应Firma
。 如果您确定姓氏和姓名在所有索引中的分隔方式相同,我们可以轻松地使用 str_split
分隔名称。
在泛型 df
中,我假设名称构造为 Surname,Name(s)。例如,第三个指数是 F-D E 女士,即。她的名字是 F-D,姓 E,头衔是女士
然后我们可以运行下面的代码,
for (i in 1:nrow(df)){
# Extract the Inv_name
# as a matrix; this will be our pattern search
# along the Firma
tmpName <- str_to_lower(
str_split(df$id[i],pattern = ",",simplify = T)
)
# Create a new variable that counts the number of
# occurences of the id
df$count1[i] <- sum(
str_detect(
string = str_to_lower(df$string),pattern = paste(tmpName,sep = "",collapse = "|")
)
)
# NOTE: That it it cannot count correctly
# due to titles like Dr. Mr. and MS.
# If you are certaing about the pattern on
# how these titles are generated then we can ignore these.
# Here we remove all matches on the form Dr. Mr. Ms.
tmpName <- str_remove_all(
tmpName,pattern = "[:alpha:]+[:punct:][:blank:]+"
)
# We then recount without titles
# to avoid confusion
df$count2[i] <- sum(
str_detect(
string = str_to_lower(df$string),collapse = "|")
)
)
}
产生以下结果,
id string count1 count2
1 Dr. A,D ABC Bankrupt 2 3
2 Mr. B,C ADEF LTD 1 1
3 Ms. E,D D GMBH 2 2
请注意,在 count1
中有一个错误,算法错过了 string
中的所有 A,因为最初该算法分别比较 Dr. A
和 D
。这会产生意想不到的结果(因为我假设 Dr.
是一个通用标题,您不想计算)。因此,我们需要从 Dr.
中删除 Dr. A
。
如果您知道所有标题的构造相似,那么这很容易解决。 IE。如果您 100% 确定 所有 标题都采用 字母后跟 . 的形式,那么我们可以通过 str_remove_all
轻松删除它。这就是 count2
中所做的。
注意事项
- 该算法会分别处理多个姓名,例如对于 E 女士,她的姓名 F 和 D 会被单独处理。
- 它只对一个字符串计数一次,因此如果字符串中有多个匹配项,则计数为一个。这可以防止过度计数。
- 假设所有标题后跟
.
进一步文献
我非常怀疑这是否能完全解决您的问题,因为您提供的数据是一个不幸的样本,因为根本没有匹配项可供测试。
因此,我建议您使用以下备忘单,以进一步了解正在发生的事情;
https://evoldyn.gitlab.io/evomics-2018/ref-sheets/R_strings.pdf
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。