【发布时间】:2018-11-12 20:26:45
【问题描述】:
我的数据集包含一些我正在尝试清理其域的电子邮件地址:
id <- c(1,2,3,4,5)
email <- c('jim@chase.com','steve@aol.com','stacy@gmail.com/','chris@yahoo.com','emilio@verizon.net/')
sample <- data.frame(id,email)
我正在尝试根据包含域的导入 .txt 文件删除行;例如,domains.txt 包含
chase.com verizon.net
我阅读了 .txt 文件
domains <- read_file('C:\\\\me\\domains.txt')
domains <- strsplit(domains, ' ')
但是我不知道如何成功地清理这些。我尝试了两种解决方案,一种带有regex 代码,另一种没有:
sample <- sample[!(paste0('^',domains,'$') %in% sample$email)]
sample$domains <- grepl(paste0('^',domains,'$'),sample$email)
sample <- subset(sample, domains == FALSE, select = c(id,email))
第一个将我的数据转换为 tibble(每当我尝试显示它时都会导致 Column indexes must be at most 4 if positive, not 5, 6, 7, 8, 9, 10 错误),而第二个为所有域返回 FALSE,包括那些包含在 domains 中列出的域的域变量。
当字符串在变量的任一侧也有需要读取的文本时,如何使用变量创建“搜索和销毁”regex?
【问题讨论】:
-
是
read_file来自readr吗? -
您是要删除域,还是删除找到这些域的观察结果?
-
它是 readr::read_file。我正在尝试删除包含这些域的案例。