Remove special characters from entire dataframe in R

PizzaAndCode picture PizzaAndCode · Apr 17, 2018 · Viewed 7.1k times · Source

Question:

How can you use R to remove all special characters from a dataframe, quickly and efficiently?

Progress:

This SO post details how to remove special characters. I can apply the gsub function to single columns (images 1 and 2), but not the entire dataframe.

Problem:

My dataframe consists of 100+ columns of integers, string, etc. When I try to run the gsub on the dataframe, it doesn't return the output I desire. Instead, I get what's shown in image 3.

df <- read.csv("C:/test.csv")
dfa <- gsub("[[:punct:]]", "", df$a) #this works on a single column
dfb <- gsub("[[:punct:]]", "", df$b) #this works on a single column
df_all <- gsub("[[:punct:]]", "", df) #this does not work on the entire df
View(df_all)

df - This is the original dataframe:

Original dataframe

dfa - This is gsub applied to column b. Good!

gsub applied to column b

df_all - This is gsub applied to the entire dataframe. Bad!

gsub applied to entire dataframe

Summary:

Is there a way to gsub an entire dataframe? Else, should an apply function be used instead?

Answer

Ryan picture Ryan · Apr 17, 2018

Here is a possible solution using dplyr:

# Example data
bla <- data.frame(a = c(1,2,3), 
              b = c("fefa%^%", "fes^%#$%", "gD%^E%Ewfseges"), 
              c = c("%#%$#^#", "%#$#%@", ",.,gdgd$%,."))

# Use mutate_all from dplyr
bla %>%
  mutate_all(funs(gsub("[[:punct:]]", "", .)))

  a           b    c
1 1        fefa     
2 2         fes     
3 3 gDEEwfseges gdgd