ARSavvy
ARSavvy

Reputation: 13

How to leave only characters in field

Im fairly new to the R language.

I have a field as a factor with the code and name of the employee.

What line of command will remove the code and only leave the Name of the Emp from the PastActivity table.

Name

C4241463 - Hadya, Susan

C4315634 - Sarah, S 

Anticipated Result

Hadya, Susan

Sarah, S 

Ive tried using the sub command to remove the string upto the point of the space after the hyphen but still no luck

gsub("C[0-9]\\- ", "", PastActivity$Name)

Upvotes: 1

Views: 41

Answers (1)

Derek Corcoran
Derek Corcoran

Reputation: 4082

Or using stringr you can use

library(stringr)
x <- c("C4241463 - Hadya, Susan", "C4315634 - Sarah, S")
str_trim(str_split(x, "-", simplify = TRUE)[,2])

Here str_split is splitting in "-", and then str_trim is trimming the white space

Upvotes: 1

Related Questions