0

A space is present in front of some of my variables, across rows and columns. I would like to replace all variables that contain a space, by the same variable without the space. I could do it for each of the variables but it is quite fastidious and I am sure there is a more elegant way of doing it.

df$var[df$var == " X"] <- "X"

I did this for each of my variables and for each of the levels of the variables. Could I write a function that automatically removes the space?

Emilie
  • 11
  • 2

2 Answers2

0

One way to go about it:

x <- c( " x", " y", " z")
x
# [1]  " x" " y" " z"
y <- gsub("[[:space:]]", "", x) 
y  
#[1] "x" "y" "z"  
deepseefan
  • 3,701
  • 3
  • 18
  • 31
0

You can have a look at the function sapply
Basically, you could try something like this: df=sapply(df, function(x) f(x)) with f the function you wanna apply to all your columns; in your case something removing spaces if I understand well.

I'm not sure about the performance though. Because what you propose is not that bad, To improve your performance in general, I'd recommand you to use datatables and not dataframes anymore (even with dplyr).

Hope it's gonna help you.

Mez13
  • 168
  • 1
  • 3
  • 13