Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

How can I show which special character was a match in each row of the single column dataframe?

The below is my dataframe:

a <- data.frame(name=c("foo","bar'","ip_sum","four","#","2_planet!","@abc!!"))

And for determining(if the string has a special character or not), I have written the following:

a$name_cleansed <- gsub("([-./&,])|[[:punct:]]","\\1",a$name) #\\1 puts back the exception we define (dash and slash)

a <- a %>% mutate(has_special_char=if_else(name==name_cleansed,FALSE,TRUE)) 

                                                                                     enter image description here 

1 Answer

0 votes
by (108k points)

I think in this case you have to use the str_extract from that you can extract the first special character.

library(stringr)

str_extract(a$name,'[[:punct:]]')

#[1] NA  "'" "_" NA  "%" "_" "@"

If you want  all of the special characters, in that case use str_extract_all.

sapply(str_extract_all(a$name,'[[:punct:]]'), function(x) toString(unique(x)))

#[1] ""     "'"    "_"    ""     "%"    "_, !" "@, !"

And to exclude some symbols, use the below code:

exclude_symbol <- c('-', '.', '/', '&', ',')

sapply(str_extract_all(a$name,'[[:punct:]]'), function(x) 

                       toString(setdiff(unique(x), exclude_symbol)))

If you are a beginner and want to know more R then do check out the R programming course that will help you in understanding R from scratch. 

Browse Categories

...