我有一个包含超过 100K 名称和地址的表。我想过滤表格以仅保留那些我认为不是垃圾邮件的电子邮件。
我有这样的地址示例
[email protected]
[email protected]
[email protected]
我现在想过滤那些在
@
符号之前只有数字的地址,以及那些在 @
之后、后缀 .com
之前只有数字的电子邮件。
我知道我可以使用
str_split
和 grepl
提取它们,但我无法将它们放入 filter
查询中以将它们从表中删除。
pattern <- "[email protected]"
str_split(pattern, '@') # this will split the address based on the sumbol
str_split(string = str_split(pattern, '@')[[1]][2], pattern = "\\.") # this will split the doamin name based on the dot separating the suffix from the numbers.
as.numeric(str_split(string = str_split(pattern, '@')[[1]][2], pattern = "\\.")[[1]][1]) # This for example will check if the string extracted above contains only numbers, if not it will return NA
但是如何将其组合到
tidyverse
查询中?
谢谢
附注 我知道这是一个牵强的问题,但是有没有某种可以在 R 中使用的电子邮件地址垃圾邮件过滤器?
我认为这种模式应该可以帮助您根据您的情况识别垃圾邮件。
(\\d+)@|@\\d+\\.com
要在
filter
中使用它,您可以使用 grepl
或 str_detect
中的 stringr
。
data %>% filter(grepl('(\\d+)@|@\\d+\\.com', email))
要获取不是垃圾邮件的行,请使用
!
否定条件。
data %>% filter(!grepl('(\\d+)@|@\\d+\\.com', email))
示例:
x <- c('[email protected]', '[email protected]', '[email protected]', '[email protected]')
grepl('(\\d+)@|@\\d+\\.com', x)
#[1] TRUE TRUE TRUE FALSE