dplyr rename first character all column names in r

To rename the first character of all column names in a data frame using dplyr, we can use the rename_all() function along with the str_to_title() function from the stringr package.

Here's an example code snippet:

main.r
library(dplyr)
library(stringr)

df <- data.frame(xyz = 1:3, abc = 4:6, qwerty = 7:9)

df %>% 
  rename_all(~str_to_title(.))
126 chars
8 lines

In this example, we first create a sample data frame df with some columns named in all lowercase. Then, we use rename_all() to apply str_to_title() function to each column name, which will capitalize the first character of every word in the name.

The resulting output will be a new data frame with the column names with the first character capitalized:

main.r
  Xyz Abc Qwerty
1   1   4      7
2   2   5      8
3   3   6      9
68 chars
5 lines

gistlibby LogSnag