library(tidyverse)

Documentation Code

This section has the code for the rename() documentation.

Example 1 - Renaming Tibble Columns

tib <- tribble(~A, ~b, ~c,
                1,  2,  3,
                4,  5,  6)
print(tib)
## # A tibble: 2 x 3
##       A     b     c
##   <dbl> <dbl> <dbl>
## 1     1     2     3
## 2     4     5     6
glimpse(tib)
## Rows: 2
## Columns: 3
## $ A <dbl> 1, 4
## $ b <dbl> 2, 5
## $ c <dbl> 3, 6
t1 <- rename(tib, a = A)

print(t1)
## # A tibble: 2 x 3
##       a     b     c
##   <dbl> <dbl> <dbl>
## 1     1     2     3
## 2     4     5     6
rename(tib, B = b, C = c)
## # A tibble: 2 x 3
##       A     B     C
##   <dbl> <dbl> <dbl>
## 1     1     2     3
## 2     4     5     6
tib <- rename(tib, 
              X1 = A,
              X2 = b,
              X3 = c)
print(tib)
## # A tibble: 2 x 3
##      X1    X2    X3
##   <dbl> <dbl> <dbl>
## 1     1     2     3
## 2     4     5     6

Example 2 - Dealing With Nonstandard Column Names

nsn <- tribble(~"flat rate", ~"2cylinder", ~"price_in_$",
                     1,           2,            3,
                     4,           5,            6)
print(nsn)
## # A tibble: 2 x 3
##   `flat rate` `2cylinder` `price_in_$`
##         <dbl>       <dbl>        <dbl>
## 1           1           2            3
## 2           4           5            6
glimpse(nsn)
## Rows: 2
## Columns: 3
## $ `flat rate`  <dbl> 1, 4
## $ `2cylinder`  <dbl> 2, 5
## $ `price_in_$` <dbl> 3, 6
#Interestingly, this formats column names with backtics.
rename(nsn,
       flat_rate       = "flat rate",
       cylinder2        = "2cylinder",
       price_in_dollars = "price_in_$")
## # A tibble: 2 x 3
##   flat_rate cylinder2 price_in_dollars
##       <dbl>     <dbl>            <dbl>
## 1         1         2                3
## 2         4         5                6

Variation - rename_with()

tibble_name <- rename(tibble(iris), Petal_Width = Petal.Width)
rename_with(tibble_name, str_to_upper) 
## # A tibble: 150 x 5
##    SEPAL.LENGTH SEPAL.WIDTH PETAL.LENGTH PETAL_WIDTH SPECIES
##           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
##  1          5.1         3.5          1.4         0.2 setosa 
##  2          4.9         3            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           3.6          1.4         0.2 setosa 
##  6          5.4         3.9          1.7         0.4 setosa 
##  7          4.6         3.4          1.4         0.3 setosa 
##  8          5           3.4          1.5         0.2 setosa 
##  9          4.4         2.9          1.4         0.2 setosa 
## 10          4.9         3.1          1.5         0.1 setosa 
## # ... with 140 more rows
rename_with(tibble_name, str_to_lower)
## # A tibble: 150 x 5
##    sepal.length sepal.width petal.length petal_width species
##           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
##  1          5.1         3.5          1.4         0.2 setosa 
##  2          4.9         3            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           3.6          1.4         0.2 setosa 
##  6          5.4         3.9          1.7         0.4 setosa 
##  7          4.6         3.4          1.4         0.3 setosa 
##  8          5           3.4          1.5         0.2 setosa 
##  9          4.4         2.9          1.4         0.2 setosa 
## 10          4.9         3.1          1.5         0.1 setosa 
## # ... with 140 more rows
rename_with(tibble_name, str_to_title) 
## # A tibble: 150 x 5
##    Sepal.length Sepal.width Petal.length Petal_width Species
##           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
##  1          5.1         3.5          1.4         0.2 setosa 
##  2          4.9         3            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           3.6          1.4         0.2 setosa 
##  6          5.4         3.9          1.7         0.4 setosa 
##  7          4.6         3.4          1.4         0.3 setosa 
##  8          5           3.4          1.5         0.2 setosa 
##  9          4.4         2.9          1.4         0.2 setosa 
## 10          4.9         3.1          1.5         0.1 setosa 
## # ... with 140 more rows

Extra Material

More On rename_with()

T <- iris %>% tibble() %>% rename_with(toupper) #base-R toupper
colnames(T)  #see next section
## [1] "SEPAL.LENGTH" "SEPAL.WIDTH"  "PETAL.LENGTH" "PETAL.WIDTH"  "SPECIES"

We can pass expressions to rename_with() using placeholder notation in which an expression is preceded by a tilde and uses the period symbol to indicate the place for the argument.

T %>% rename_with(~str_to_upper(.) )
## # A tibble: 150 x 5
##    SEPAL.LENGTH SEPAL.WIDTH PETAL.LENGTH PETAL.WIDTH SPECIES
##           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
##  1          5.1         3.5          1.4         0.2 setosa 
##  2          4.9         3            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           3.6          1.4         0.2 setosa 
##  6          5.4         3.9          1.7         0.4 setosa 
##  7          4.6         3.4          1.4         0.3 setosa 
##  8          5           3.4          1.5         0.2 setosa 
##  9          4.4         2.9          1.4         0.2 setosa 
## 10          4.9         3.1          1.5         0.1 setosa 
## # ... with 140 more rows

Note that str_to_title() doesnโ€™t treat periods as blanks.

iris <- tibble(iris)
iris %>% rename_with(str_to_title) #Sepal.Length -> Sepal.length
## # A tibble: 150 x 5
##    Sepal.length Sepal.width Petal.length Petal.width Species
##           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
##  1          5.1         3.5          1.4         0.2 setosa 
##  2          4.9         3            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           3.6          1.4         0.2 setosa 
##  6          5.4         3.9          1.7         0.4 setosa 
##  7          4.6         3.4          1.4         0.3 setosa 
##  8          5           3.4          1.5         0.2 setosa 
##  9          4.4         2.9          1.4         0.2 setosa 
## 10          4.9         3.1          1.5         0.1 setosa 
## # ... with 140 more rows

Here is a complicated example that solves the problem of the str_to_title() function not recognizing underscores and periods as blanks.

iris %>% 
  rename_with(~str_replace(string = .,
                           pattern="[.]",
                           replacement= " ")) %>% 
  rename_with(~str_to_title(.)) %>% 
  rename_with(~str_replace(string = .,
                           pattern="[ ]",
                           replacement= "_")) %>% 
  colnames #See next section
## [1] "Sepal_Length" "Sepal_Width"  "Petal_Length" "Petal_Width"  "Species"

The colnames() Function

The colnames() function returns a character vector of column names.

colnames(mtcars)
##  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
## [11] "carb"

Changing the column names vector changes the column names of the original.

df <- iris
colnames(df)
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"
colnames(df) <- str_replace(colnames(df),"[.]","_")
colnames(df)
## [1] "Sepal_Length" "Sepal_Width"  "Petal_Length" "Petal_Width"  "Species"

Challenge Question

Consider the data frame

  • my_frame <- data.frame(F=1:3, G=1:3) #3 rows, 2 columns

In what important way do the following two lines of code differ in their effects on my_frame?

  • my_frame %>% rename(newF = F, newG = G)

  • colnames(my_frame) <- c("newF","newG")

Answer: the first line creates a copy of my_frame but leaves the column names of my_frame unchanged; while the second line changes the column names of my_frame.

Miscellany

The rename() function can change the column names of a tibble or a data frame, but it cannot change the column names of a matrix or 2D-table.

If you have used the iris data frame, then you might appreciate the following renaming:

my_iris <- iris %>% rename(sepal_length = Sepal.Length,
                           sepal_width  = Sepal.Width,
                           petal_length = Petal.Length,
                           petal_width  = Petal.Width,
                           species      = Species)
colnames(my_iris)
## [1] "sepal_length" "sepal_width"  "petal_length" "petal_width"  "species"