Rename column names with tidyverse.

Hello everyone.
This is a bit of a silly question, but I cannot solve it lol.

How do I change all the column names from capital to lower case with tidyverse?
I am aware of the janitor package and I also know how do it one by one. But I am looking to do with tidyverse and for the life of me I cannot do it.

EDIT: It seems I was not completely clear. So what I want to do is to include the tolower in the pipeline, for example:

df %>% mutate(across(where(is.character), is.factor)) %>% tolower(colnames())

And it doesnt work...

Sample data frame:

testando <- structure(list(BRANCH = structure(1:3, .Label = c("A", "B", "C"
), class = "factor"), CITY = structure(c(2L, 2L, 1L), .Label = c("Naypyitaw", 
                                                                 "Yangon"), class = "factor"), GENDER = structure(c(2L, 2L, 1L
                                                                 ), .Label = c("Female", "Male"), class = "factor"), DATE = structure(c(17901, 
                                                                                                                                        17958, 17963), class = "Date"), RATING = c(9.1, 9.7, 9.6)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                        -3L))
testando <- structure(list(BRANCH = structure(1:3, .Label = c("A", "B", "C"), class = "factor"), CITY = structure(c(2L, 2L, 1L), .Label = c(
  "Naypyitaw",
  "Yangon"
), class = "factor"), GENDER = structure(c(2L, 2L, 1L), .Label = c("Female", "Male"), class = "factor"), DATE = structure(c(
  17901,
  17958, 17963
), class = "Date"), RATING = c(9.1, 9.7, 9.6)), class = "data.frame", row.names = c(
  NA,
  -3L
))
testando
#>   BRANCH      CITY GENDER       DATE RATING
#> 1      A    Yangon   Male 2019-01-05    9.1
#> 2      B    Yangon   Male 2019-03-03    9.7
#> 3      C Naypyitaw Female 2019-03-08    9.6
colnames(testando) <- tolower(colnames(testando))
testando
#>   branch      city gender       date rating
#> 1      A    Yangon   Male 2019-01-05    9.1
#> 2      B    Yangon   Male 2019-03-03    9.7
#> 3      C Naypyitaw Female 2019-03-08    9.6

Hey @technocrat , thank you for your reply! I noticed I should have added a bit more complexity to my code.

Maybe I can do it now.
Imagine I am doing the following:

testando %>% mutate( across ( where ( is.character), is.factor),
               tolower ( colnames() ) )

This doesnt work. I basically want to insert the "tolower" in the pipeline!
Thank you once again

the rename_with from dplyr might be what you want:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(reprex)

head(iris) %>% rename_with(tolower, .cols = where(is.factor))
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa

Created on 2021-06-01 by the reprex package (v2.0.0)

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.