Removing Whitespace From a Whole Data Frame in R

Viewed 110458

I've been trying to remove the white space that I have in a data frame (using R). The data frame is large (>1gb) and has multiple columns that contains white space in every data entry.

Is there a quick way to remove the white space from the whole data frame? I've been trying to do this on a subset of the first 10 rows of data using:

gsub( " ", "", mydata) 

This didn't seem to work, although R returned an output which I have been unable to interpret.

str_replace( " ", "", mydata)

R returned 47 warnings and did not remove the white space.

erase_all(mydata, " ")

R returned an error saying 'Error: could not find function "erase_all"'

I would really appreciate some help with this as I've spent the last 24hrs trying to tackle this problem.

Thanks!

10 Answers

A lot of the answers are older, so here in 2019 is a simple dplyr solution that will operate only on the character columns to remove trailing and leading whitespace.

library(dplyr)
library(stringr)

data %>%
  mutate_if(is.character, str_trim)

## ===== 2020 edit for dplyr (>= 1.0.0) =====
df %>% 
  mutate(across(where(is.character), str_trim))

You can switch out the str_trim() function for other ones if you want a different flavor of whitespace removal.

# for example, remove all spaces
df %>% 
  mutate(across(where(is.character), str_remove_all, pattern = fixed(" ")))

One possibility involving just dplyr could be:

data %>%
 mutate_if(is.character, trimws)

Or considering that all variables are of class character:

data %>%
 mutate_all(trimws)

Since dplyr 1.0.0 (only strings):

data %>%
 mutate(across(where(is.character), trimws))

Or if all columns are strings:

data %>%
 mutate(across(everything(), trimws))

I think that a simple approach with sapply, also works, given a df like:

dat<-data.frame(S=LETTERS[1:10],
            M=LETTERS[11:20],
            X=c(rep("A:A",3),"?","A:A ",rep("G:G",5)),
            Y=c(rep("T:T",4),"T:T ",rep("C:C",5)),
            Z=c(rep("T:T",4),"T:T ",rep("C:C",5)),
            N=c(1:3,'4 ','5 ',6:10),
            stringsAsFactors = FALSE)

You will notice that dat$N is going to become class character due to '4 ' & '5 ' (you can check with class(dat$N))

To get rid of the spaces on the numeic column simply convert to numeric with as.numeric or as.integer.

dat$N<-as.numeric(dat$N)

If you want to remove all the spaces, do:

dat.b<-as.data.frame(sapply(dat,trimws),stringsAsFactors = FALSE)

And again use as.numeric on col N (ause sapply will convert it to character)

dat.b$N<-as.numeric(dat.b$N)
Related