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
?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With