-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Could a str_to_snake
function be made to convert strings to snake-case?
#573
Comments
Hmmmm, we'd probably want to add |
I think |
I have a student (@librill) who would like to tackle this. We see that the existing So, would the preferred approach be:
|
I think you could just implement it in R, using a regular expression or other stringr functions. |
I'm not a stringr dev so I'm just giving my two cents. I believe that the cost of adding janitor as a dependency is marginal and outweighs the time spent to write There's also snakecase (the actual package that janitor relies on to convert strings to particular case types) which is relatively lightweight and only depends on stringr and stringi. That said, I have an old to_snake_case <- function(x) {
x <- stringr::str_replace_all(x, c(
r"{[^\p{L}\p{N}]+|(?<=\p{Lu})(?=\p{Lu}\p{Ll})|(?<=\p{Ll})(?=\p{Lu}|\p{N})|(?<=\p{N})(?!\p{N})}" = "_",
"^_|_$" = ""
))
tolower(x)
} Removing the raw string will make the function slightly faster. I always thought that using regex for this wasn't ideal but I'm sure Hadley knows better than me. |
It'd be useful to have a
str_to_snake
function in stringr.I think the main use for this would be to rename column names to snake-case:
rename_with(data, stringr::str_to_snake)
It would enable not taking on another package dependency with
janitor::clean_names
in many user's workflowsThe text was updated successfully, but these errors were encountered: