Reputation:
I want to transform a vector of adresses that look like this [email protected]
into a dataframe that contains 3 variables firstname
lastname
and adress
.
this is the vector I want to transform emails <- c("[email protected]", "[email protected]", "[email protected]")
What function/functions should I use?
EDIT : I'm supposed to create a function that takes an email as input and return a dataframe.
Upvotes: -1
Views: 31
Reputation: 5017
Using basic R code:
str<-c("[email protected]","[email protected]")
out_df<-NULL
out_df$first<-unlist(lapply(strsplit(sub("\\@.*", "", str),"[.]"), `[[`, 1))
out_df$last<-unlist(lapply(strsplit(sub("\\@.*", "", str),"[.]"), `[[`, 2))
out_df$domain<-sub('.*@', '', str)
data.frame(out_df)
first last domain
1 Firstname Lastname bla.com
2 Firstname2 Lastname2 bla2.com
Here the function format:
f<-function(str)
{
first<-unlist(lapply(strsplit(sub("\\@.*", "", str),"[.]"), `[[`, 1))
last<-unlist(lapply(strsplit(sub("\\@.*", "", str),"[.]"), `[[`, 2))
domain<-sub('.*@', '', str)
return(data.frame(cbind(first,last,domain)))
}
Upvotes: 1
Reputation: 887881
We can use extract
library(tibble)
library(tidyr)
tibble(emails) %>%
extract(emails, into = c("firstname", "lastname", "address"),
"^(\\w+)\\.([^@]+)@(.*)")
-output
# A tibble: 3 x 3
# firstname lastname address
# <chr> <chr> <chr>
#1 fname1 lname1 bla.com
#2 fname2 lname2 bla.com
#3 fname3 lname3 bla.com
If we need a base R
option, either use strsplit
or read.csv
f1<- function(vec) {
read.csv(text = sub("^(\\w+)\\.([^@]+)@(.*)", "\\1,\\2,\\3",
vec), header = FALSE, col.names = c('firstname', 'lastname', 'address'))
}
f1(emails)
Upvotes: 3