【发布时间】:2016-01-12 17:10:45
【问题描述】:
我有一个基本上我想清理的电子邮件列表。我想声明,如果“@”字符不在特定的电子邮件中,我想删除该电子邮件 - 这样一来,“mywebsite.com”之类的输入就会被删除。
我的代码如下:
email_clean <- function(email, invalid = NA){
email <- trimws(email) # Removes whitespace
email[(nchar(email) %in% c(1,2)) ] <- invalid # Removes emails with 1 or 2 character length
bad_email <- c("\\@no.com", "\\@na.com","\\@none.com","\\@email.com", # List of bad emails - modify to the
"\\@noemail.com", "\\@test.com", # specifications of the request
pattern = paste0("(?i)\\b",paste0(bad_email,collapse="\\b|\\b"),"\\b") # Deletes names matching bad email
email <-gsub(pattern, invalid, sapply(email,as.character))
unname(email)
}
## Define vector of SSN from origianl csv column
Cleaned_Email <- email_clean(my_data$Email)
## Binds cleaned phone to csv
my_data<-cbind(my_data,Cleaned_Email)
谢谢!!
【问题讨论】:
-
你有什么问题?