我试图获取一个包含profiles
地址列的数据框email
,并添加一个由每个电子邮件地址的可注册域部分组成的新列{ {1}}。
我分别创建一个唯一domain
的向量,这个过程太复杂而无法对数据框中的每一行运行,其结果是一个必然小于行数的向量在registerable_domains
数据框中。然后,我会检查profiles
向量中的每个条目是否都显示在registerable_domains
数据框中每个email
地址的末尾,并设置profiles
列的domain
列条目存在匹配的数据框。
下面的代码是可复制的数据,您可以在R中复制粘贴和执行,每行注释以解释它的作用。
for()
循环完全符合我的要求:它会在domain
数据框的profiles
列中创建相应的条目。问题在于,在此示例中,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
答案 0 :(得分:3)
以下是使用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
答案 1 :(得分:1)
我认为你可以通过简单的水果和一些易于矢量化的for
循环中的一些操作来显着减少你的时间。
profiles <- profiles %>% mutate(test_domains = sub(".*@", "", email))
很简单,只是为您提供了一个新列,而不是在每次迭代中花费时间。
for (d in registerable_domains){
profiles$domain[d == profiles$test_domains] <- d
}
将采取直接匹配,并且应该只为那些仍然具有NA
的行提供现在的昂贵循环,即
profiles[is.na(profiles$domain)]
这将是一个合适的子集。我不知道它能拯救你多少,我现在必须要去。我将回到这一点。感谢您提供详尽的数据问题。
答案 2 :(得分:0)
不确定这是否有用,因为我完全改变了for循环的理念及其作用。此外,我没有意识到你是否真的需要可注册的域名。但是,我的想法是使用这些域所具有的模式并将其应用于您的电子邮件列表,而不是拥有可注册域名列表。
例如,如果域名以com
或ca
结尾,那么您保留此部分以及左侧的内容,例如searchfor@janedoe.com
变为janedoe.com
。如果域名以uk
结尾,那么您需要此部分,还需要co
以及之前的内容。
如果您设法发现这些模式,您可以使用if-else规则创建一个简单的函数,并执行类似
的操作x = c("luke@star.wars.com",
"area@toronto.canada.ca",
"mary@smith.co.uk")
dt = data.frame(x, stringsAsFactors = F)
dt
# x
# 1 luke@star.wars.com
# 2 area@toronto.canada.ca
# 3 mary@smith.co.uk
ff = function(x){
x = strsplit(x, split = "[[:punct:]]")[[1]]
ifelse(x[length(x)] %in% c("com","ca"),
paste(x[(length(x)-1):length(x)], collapse = "."),
paste(x[(length(x)-2):length(x)], collapse = "."))}
dt$v = sapply(dt$x, ff)
dt
# x v
# 1 luke@star.wars.com wars.com
# 2 area@toronto.canada.ca canada.ca
# 3 mary@smith.co.uk smith.co.uk