martinctc
10/9/2017 - 12:09 PM

Clean strings so that they can be used as variable names or column names

[Clean Strings] Clean strings so that they can be used as variable names or column names #R

clean_strings <-
  function(string){
    new_string <- string %>% gsub("'", "", .) %>% gsub("\"", 
                                                       "", .) %>% gsub("%", "percent", .) %>% gsub("^[ ]+", 
                                                                                                   "", .) %>% make.names(.) %>% gsub("[.]+", "_", .) %>% 
      gsub("[_]+", "_", .) %>% tolower(.) %>% gsub("_$", "", 
                                                   .)
    dupe_count <- sapply(1:length(new_string), function(i) {
      sum(new_string[i] == new_string[1:i])
    })
    
    new_string[dupe_count > 1] <- paste(new_string[dupe_count > 
                                                     1], dupe_count[dupe_count > 1], sep = "_")
    new_string
  }