gpt4 book ai didi

regex - 替代 for() 循环以将非常大的数据框列条目与非常大的向量列表进行比较

转载 作者:行者123 更新时间:2023-12-04 02:17:24 27 4
gpt4 key购买 nike

我正在尝试获取一个数据框 profiles,其中包含一列 email 地址,并添加一个由每个地址的可注册域部分组成的新列电子邮件地址、

我单独创建了一个独特的 registerable_domains 向量,这个过程太复杂以至于无法针对数据框中的每一行运行,其结果是一个必然小于profiles 数据框中的行数。然后我检查 registerable_domains 向量中的每个条目是否出现在 profiles 数据框中每个 email 地址的末尾,并设置匹配的数据框的 domain 列条目。

下面的代码是可重现的数据,您可以在 R 中复制粘贴并执行,每行都带有注释以解释它的作用。

for() 循环做的正是我想做的:它在 profiles 数据的 domain 列中创建适当的条目框架。问题在于,在此示例中,profiles 数据框有 12 行,而 registerable_domains 向量有 8 个条目。在实际数据集中,profiles 数据框有大约 500,000 行,registerable_domains 向量有大约 110,000 个条目。因此,虽然 for() 循环在小数据集上工作得很好,但我需要一种不同的方法来处理非常大的数据集(我估计这种方法需要大约 75 年才能完成)在完整的数据集上完成!)。

非常感谢您帮助将此 for() 循环转换为大型数据集的时间实际操作。我查看了许多其他线程,但找不到解决此特定情况的任何答案(尽管解决了许多其他类似但不同的情况)。谢谢!

# Data frame consisting of a column of 12 emails, and a column of 12 NA entries:

email <- c( "john@doe.com",
"mary@smith.co.uk",
"peter@microsoft.com",
"jane@admins.microsoft.com",
"luke@star.wars.com",
"leia@star.wars.com",
"yoda@masters.star.wars.com",
"grandma@bletchly.ww2.wars.com",
"searchfor@janedoe.com",
"fan@mail.starwars.com",
"city@toronto.ca",
"area@toronto.canada.ca");

domain <- c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA);

profiles <- data.frame(email, domain);

profiles; # See what the initial data frame looks like

# email domain
# 1 john@doe.com NA
# 2 mary@smith.co.uk NA
# 3 peter@microsoft.com NA
# 4 jane@admins.microsoft.com NA
# 5 luke@star.wars.com NA
# 6 leia@star.wars.com NA
# 7 yoda@masters.star.wars.com NA
# 8 grandma@bletchly.ww2.wars.com NA
# 9 searchfor@janedoe.com NA
# 10 fan@mail.starwars.com NA
# 11 city@toronto.ca NA
# 12 area@toronto.canada.ca NA

# Vector consisting of email addresses stripped to registerable domain component only, created through a separate process that is too complex to run on each row entry:

registerable_domains <- c( "doe.com",
"smith.co.uk",
"microsoft.com",
"wars.com",
"janedoe.com",
"starwars.com",
"toronto.ca",
"canada.ca");

# Credit to Nick Kennedy for his help with this original solution (http://stackoverflow.com/users/4998761/nick-kennedy)

for (domains in registerable_domains) { # Iterate through each of the registerable domains
domains_pattern <- paste("[.@]", domains, "$", sep=""); # Add regex characters to ensure that it's only the end part to deal with nested domain names
found <- grepl(domains_pattern, profiles$email, ignore.case=TRUE, perl=TRUE); # Grep for the current domain pattern in all of the emails and build a boolean table for entry locations
profiles[which(found & is.na(profiles$domain)), "domain"] <- domains; # Modify profile data table at TRUE entry locations not yet set
}

profiles; # Expected and desired outcome:

# email domain
# 1 john@doe.com doe.com
# 2 mary@smith.co.uk smith.co.uk
# 3 peter@microsoft.com microsoft.com
# 4 jane@admins.microsoft.com microsoft.com
# 5 luke@star.wars.com wars.com
# 6 leia@star.wars.com wars.com
# 7 yoda@masters.star.wars.com wars.com
# 8 grandma@bletchly.ww2.wars.com wars.com
# 9 searchfor@janedoe.com janedoe.com
# 10 fan@mail.starwars.com starwars.com
# 11 city@toronto.ca toronto.ca
# 12 area@toronto.canada.ca canada.ca

最佳答案

这是一个使用 dplyr

的解决方案
library(dplyr)
person <- data_frame(Email = email) %>%
mutate(Domain = gsub("^.*@", "", Email)) # everything upto the last @
domain <- person %>%
select(Domain) %>% # select the Domain variable
distinct() %>% # keep only unique rows
mutate(Original = Domain) # copy Domain into Original
extra <- domain %>%
mutate(Domain = gsub("^[[:alnum:]]*\\.", "", Domain)) %>% # remove all alphanumeric characters upto the first point and overwrite Domain
filter(grepl("\\.", Domain)) # keep only observations where domain contains at least one point
while (nrow(extra) > 0){
domain <- bind_rows(domain, extra) #add the rows from extra to domain
extra <- extra %>%
mutate(Domain = gsub("^[[:alnum:]]*\\.", "", Domain)) %>%
filter(grepl("\\.", Domain))
}
register <- data_frame(Domain = registerable_domains)
register %>%
inner_join(domain, by = "Domain") %>% #join the two table on a common Domain
inner_join(person, by = c("Original" = "Domain")) # join the resulting table to person where result.Original = person.Domain

关于regex - 替代 for() 循环以将非常大的数据框列条目与非常大的向量列表进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33090495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com