2016-12-25 85 views
1

我想從我的數據框的「收件人」列中提取特定電子郵件(@ 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

+0

你可以分享你的輸入數據樣本和所需的輸出? – Psidom

回答

1

我們可以使用grep這個

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

如果有單排多封電子郵件,使用str_extract

library(stringr) 
data.frame(To =sapply(str_extract_all(df$To, "\\[email protected]"), paste, collapse=",")) 
+1

非常感謝。 –

相關問題