【问题标题】:Extract specific emails from different emails in a column- R从列中的不同电子邮件中提取特定电子邮件 - R
【发布时间】:2016-12-25 00:11:47
【问题描述】:

我想从我的数据框中的“收件人”列中提取特定的电子邮件 (@enron.com)。在某些行中,有不止一封电子邮件。例如,在一排我有这个:mark.guzman@enron.com, creightonca@hotmail.com, brendanf@gfsloans.com, seastape@teleport.com, penn_eric@salkeiz.k12.or.us,joe.stepenovitch@enron.com, jan.king@enron.com。我的问题是如何从该列中仅提取安然域 (@enron.com) 电子邮件并将其保存在新列中?我可以提取它们,但问题是它将每封电子邮件放在不正确的行中,因为例如,如果一行包含 20 封电子邮件中的 10 封安然电子邮件我希望将所有安然电子邮件放在一行而不是 10 行中。我从这里运行代码:How to extract expression matching an email address in a text file using R or Command Line?emails = regmatches(df, gregexpr("([_a-z0-9-]+(\\.[_a-z0-9-]+)*@enron.com)", df))但我收到此错误:Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 1, 2, 0, 5

【问题讨论】:

  • 您能分享一下您的输入数据和所需输出的样本吗?

标签: r email text-extraction


【解决方案1】:

我们可以为此使用grep

subset(df, grepl("enron.com", To))

如果一行中有多个电子邮件,请使用str_extract

library(stringr)
data.frame(To =sapply(str_extract_all(df$To, "\\S+@enron.com"), paste, collapse=","))

【讨论】:

  • 非常感谢。
猜你喜欢
  • 2021-12-23
  • 1970-01-01
  • 2016-04-12
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
  • 1970-01-01
相关资源
最近更新 更多