Sometimes when you are coding something you need to make a test This is a test It is only a test of the gsub function

#install.packages("stringr")
library(stringr)

Creating test dataframe for removing punctuation

email <- c('phillip.allen@yahoo.com', 'bruce.wayne@gmail.com', 'clark.kent@gmail.com', 'mary.smith@yahoo.com')
test_emails <- data.frame(email)

print(test_emails)
##                     email
## 1 phillip.allen@yahoo.com
## 2   bruce.wayne@gmail.com
## 3    clark.kent@gmail.com
## 4    mary.smith@yahoo.com

We’re going to split at the ‘@’ Function

test_emails$names  <- str_extract(test_emails$email, "^[^@]*(?=@)")

head(test_emails)
##                     email         names
## 1 phillip.allen@yahoo.com phillip.allen
## 2   bruce.wayne@gmail.com   bruce.wayne
## 3    clark.kent@gmail.com    clark.kent
## 4    mary.smith@yahoo.com    mary.smith

Testing string replace on test dataframe

test_emails <- gsub("[[:punct:]]", " ", test_emails$names)

Returning it back to a dataframe

test <-data.frame(test_emails)
head(test)
##     test_emails
## 1 phillip allen
## 2   bruce wayne
## 3    clark kent
## 4    mary smith