rjanitor

Change behavior of clean_names (janitor) to remove underscore


I am cleaning names of a data frame from an imported XLS file that is named Concentration \n(ng/mL):

test_data <- tibble(
  subject = 1,
  `Concentration \n(ng/mL)` = 10
)

test_data_clean <- janitor::clean_names(test_data)

names(test_data_clean)

[1] "subject"              "concentration_ng_m_l"

How do I change the behavior of clean_names so that it outputs the following instead?

concentration_ng_ml

clean_names seems to be interpreting the upper case "L" after a lower case "m". Is there any way to change this rule?


Solution

  • You can specify a named character vector where the name is replaced by the vector. So you could try the following:

    library(janitor)
    
    clean_names(test_data, replace = c("mL" = "ml")) 
    
      subject concentration_ng_ml
        <dbl>               <dbl>
    1       1                  10
    

    Alternatively, you could experiment with different case arguments. For example, you can use case = "none":

    clean_names(test_data, case = "none") 
    
      subject Concentration_ng_mL
        <dbl>               <dbl>
    1       1                  10 
    

    This might be a good option if you didn't want all true snake case.