我有一个基本上我想要清理的电子邮件列表。我想声明如果'@'字符不在特定电子邮件中,我想删除该电子邮件 - 这样就会删除像'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)
谢谢!
答案 0 :(得分:3)
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
email[!grepl("@", email)] <- invalid # <------------------ New line added here ------------
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)
}
答案 1 :(得分:0)
尝试此选项可以排除my_data中没有&#39; @&#39;的所有行。登录电子邮件列:
my_data <- my_data[grep('@', my_data$Email), ]