Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting a certain substring (email address)

Tags:

substring

regex

r

I'm attempting to pull some a certain from a variable that looks like this:

v1 <- c("Persons Name <[email protected]>","person 2 <[email protected]>")

(this variable has hundreds of observations)

I want to eventually make a second variable that pulls their email to give this output:

v2 <- c("[email protected]", "[email protected]")

How would I do this? Is there a certain package I can use? Or do I need to make a function incorporating grep and substr?

like image 521
Mr. Biggums Avatar asked Aug 20 '19 20:08

Mr. Biggums


People also ask

How do I extract multiple email addresses from one cell?

In Excel, the Text to Columns function also can do you a favor on separating email addresses. Select the email addresses you want to separate, and click Data > Text to Columns. Then in the Text to Columns Wizard window, check Delimited option and click Next to go the step 2 of the Wizard.

How do you select a substring from a string?

The substring() method extracts characters, between two indices (positions), from a string, and returns the substring. The substring() method extracts characters from start to end (exclusive). The substring() method does not change the original string.


1 Answers

Those look like what R might call a "person". There is an as.person() function that can split out the email address. For example

v1 <- c("Persons Name <[email protected]>","person 2 <[email protected]>")
unlist(as.person(v1)$email)
# [1] "[email protected]" "[email protected]"

For more information, see the ?person help page.

like image 85
MrFlick Avatar answered Sep 21 '22 21:09

MrFlick