1

unfortunately I have got colnames, which all have additional single quotation marks like here:

x <- data.frame(c(11,21,31),c(12,22,32),c(13,23,33))
colnames(x) <- c("'A'","'B'","'C'")

So my question is, if I can get rid of those "'" for my entire data frame? Preferably not retyping the colnames, and using tidyR code? Thanks!

LxndrF
  • 25
  • 8

3 Answers3

3

In base R:

names(x) <- gsub("'", "", names(x))

x
   A  B  C
1 11 12 13
2 21 22 23
3 31 32 33
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
1
library(stringr)

colnames(x) <- str_remove_all(colnames(x), "'")

That should do it

library(dplyr)
library(stringr)
    x %>% 
       rename_all(~str_remove_all(., "'"))

If you want a pipe

NotThatKindODr
  • 729
  • 4
  • 14
  • Yes, it does. Thanks - Great library - I was worried I would have to use gsub! What a relief ;-) – LxndrF May 28 '20 at 17:34
0

We can use trimws in base R

names(x) <-  trimws(names(x), whitespace = "'")
x
#   A  B  C
#1 11 12 13
#2 21 22 23
#3 31 32 33
akrun
  • 874,273
  • 37
  • 540
  • 662