Home > Software engineering >  Unable to Remove 'Left Quote' Character (`) from Column names
Unable to Remove 'Left Quote' Character (`) from Column names

Time:05-25

I am experiencing problems removing the left quote " ` " character (acute, back quote, grave, grave accent, open quote, or a push are alternative names) from some of my column names.

I attempted several of the solutions provided on these pages (in code below) but with no success. Thoughts?

enter image description here


library(tidyverse)
library(stringr)


tibble_dog <- tibble (`yls23_rolled_/_nktg23_`   = 1:6,
                      `1alw23_rolled_/_ndatg23_` = 11:16,
                      `1alw6_rolled_/_ndatg24_`  = 21:26
                      )

tibble_dog


tibble_dog <- tibble_dog %>% 
  rename_with(~ str_remove(., " ` "), everything())

tibble_dog

names(tibble_dog) <- sub("`", "", names(tibble_dog))

tibble_dog

names(tibble_dog) <- trimws(colnames(tibble_dog), whitespace = "\\s \\(.*")

tibble_dog

CodePudding user response:

you could try the clean_names function from the janitor package:

library(janitor)

janitor::clean_names(tibble_dog)

CodePudding user response:

From the comments ...

The reason you see the backtick ` in the column names is because tibbles wrap column names in backticks where the name contains normally "illegal" characters (the slash / is the problem). See:

tibble("a b" = 1)
# # A tibble: 1 x 1
#   `a b`
#   <dbl>
# 1     1
tibble("a/b" = 1)
# # A tibble: 1 x 1
#   `a/b`
#   <dbl>
# 1     1
tibble("a_b" = 1)
# # A tibble: 1 x 1
#     a_b
#   <dbl>
# 1     1

Instead of trying to remove the backticks, you may consider removing the slash from the column names with

names(tibble_dog) <- sub("/", "", names(tibble_dog))
  •  Tags:  
  • r
  • Related