prayner
prayner

Reputation: 415

How to make variable names human-readable in R

Is there a dedicated function that does the opposite of janitor::clean_names and converts clean variable names to presentable names:

e.g. "my_variable_names" becomes "My variable name".

Upvotes: 1

Views: 490

Answers (2)

Adam Sampson
Adam Sampson

Reputation: 2021

janitor::make_clean_names("my_test_title",case = "sentence")
# [1] "My test title"

Edit (to match the question better):


data.frame(my_test_title = c(1)) %>% janitor::clean_names(case = "sentence")
#  My test title
#1             1

Upvotes: 6

akrun
akrun

Reputation: 887148

We could use gsub/sub or chartr to do the conversion i.e. replace the _ with space " " and then make the first letter upper case

sub("^(.)", "\\U\\1", gsub("_", " ", str1), perl = TRUE)
[1] "My variable names"

If we need a package solution, then use to_sentence_case from snakecase

library(snakecase)
to_sentence_case(str1)
[1] "My variable names"

data

str1 <- "my_variable_names"

Upvotes: 1

Related Questions