Home > Back-end >  Rename strings systematically in R
Rename strings systematically in R

Time:05-16

I have a string vector like this:

s <- c("X _1", 
       "X _2",
       "X _3",
       "X _4",
       "X _5",
       "X _6",
       "X _7",
       "X _8")

Is there a way of systematically renaming the variables such that, for example, X _1 becomes X_1?

Thanks!

CodePudding user response:

We can use sub (or gsub if more than one instance of space) to match the space and replace with blank ("")

sub(" ", "", s)
[1] "X_1" "X_2" "X_3" "X_4" "X_5" "X_6" "X_7" "X_8"
  • Related