从列中的不同电子邮件中提取特定电子邮件 - R
我想从我的数据框的“收件人”列中提取特定电子邮件(@ enron.com)。在某些行中,有多个电子邮件。例如在一行中,我有这个:[email protected], [email protected], [email protected], [email protected], [email protected],[email protected], [email protected]
。我的问题是,如何从此列中提取安然域(@ enron.com)电子邮件并将其保存到新列中?我可以提取它们,但问题是它会将每个电子邮件放在一行中,因为例如if一行包含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
我们可以使用grep
这个
subset(df, grepl("enron.com", To))
如果有单排多封电子邮件,使用str_extract
library(stringr)
data.frame(To =sapply(str_extract_all(df$To, "\\[email protected]"), paste, collapse=","))
非常感谢。 –
你可以分享你的输入数据样本和所需的输出? – Psidom