Skip to content

Commit

Permalink
handle leading spaces in variable names with clean_names
Browse files Browse the repository at this point in the history
closes #85
  • Loading branch information
sfirke committed Jan 8, 2017
1 parent 53ca562 commit ec9916f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
1 change: 1 addition & 0 deletions R/clean_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ clean_names <- function(dat){
gsub("'", "", .) %>% # remove quotation marks
gsub("\"", "", .) %>% # remove quotation marks
gsub("%", "percent", .) %>%
gsub("^[ ]+", "", .) %>%
make.names(.) %>%
gsub("[.]+", "_", .) %>% # convert 1+ periods to single _
gsub("[_]+", "_", .) %>% # fix rare cases of multiple consecutive underscores
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-clean-names.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
library(janitor)
context("data.frame name cleaning")

test_df <- data.frame(matrix(ncol = 10) %>% as.data.frame())
test_df <- data.frame(matrix(ncol = 11) %>% as.data.frame())
names(test_df) <- c("sp ace", "repeated", "a**#@", "%", "#", "!",
"d(!)9", "REPEATED", "can\"'t", "hi_`there`")
"d(!)9", "REPEATED", "can\"'t", "hi_`there`", " leading spaces")

clean <- clean_names(test_df)

Expand All @@ -20,6 +20,7 @@ test_that("Names are cleaned appropriately", {
expect_equal(names(clean)[8], "repeated_2") # uppercase, 2nd instance of repeat
expect_equal(names(clean)[9], "cant") # uppercase, 2nd instance of repeat
expect_equal(names(clean)[10], "hi_there") # double-underscores to single
expect_equal(names(clean)[11], "leading_space") # leading spaces
})

test_that("Returns a data.frame", {
Expand Down

0 comments on commit ec9916f

Please sign in to comment.