Documentation Code

This is the code for the rename() documentation.

tibble 1

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

tibble 2

tibble2 <- tribble(~`flat rate`, ~`2cylinder`, ~`price_in_$`,
                         1,           2,            3,
                         4,           5,            6)
print(tibble2)
## # A tibble: 2 x 3
##   `flat rate` `2cylinder` `price_in_$`
##         <dbl>       <dbl>        <dbl>
## 1           1           2            3
## 2           4           5            6
glimpse(tibble2)
## Rows: 2
## Columns: 3
## $ `flat rate`  <dbl> 1, 4
## $ `2cylinder`  <dbl> 2, 5
## $ `price_in_$` <dbl> 3, 6
tibble2 %>% 
   rename(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

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)

Extra Material

rename_with()

There is a variation called rename_with() that applies a renaming function to all the column names:

iris <- tibble(iris)
iris %>% rename_with(str_to_upper) #Sepal.Length becomes SEPAL.LENGTH, etc.
## # 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
iris %>% rename_with(str_to_lower) #Sepal.Length becomes sepal.length, etc.
## # 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
iris %>% rename_with(str_to_title) #Sepal.Length becomes Sepal.length, etc.
## # 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
iris %>% rename_with(toupper)  #Using a base-R function
## # 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 another way to think about using placeholder functions.

tibble1 %>% rename_with(~str_to_upper(.) ) 
## # A tibble: 2 x 3
##       A     B     C
##   <dbl> <dbl> <dbl>
## 1     1     2     3
## 2     4     5     6
tibble1 %>% rename_with(~str_to_lower(.) )
## # A tibble: 2 x 3
##       a     b     c
##   <dbl> <dbl> <dbl>
## 1     1     2     3
## 2     4     5     6
tibble1 %>% rename_with(~str_to_title(.) ) 
## # A tibble: 2 x 3
##       A     B     C
##   <dbl> <dbl> <dbl>
## 1     1     2     3
## 2     4     5     6

Placeholder allow you to specify which argument is the active one. Here is a complicated example that yields a satisfying result.

iris %>% 
  rename_with(~str_replace(string = .,
                           pattern="[.]",
                           replacement= " ")) %>% 
  rename_with(~str_to_title(.)) %>% 
  rename_with(~str_replace(string = .,
                           pattern="[ ]",
                           replacement= "_"))
## # 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

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 would change 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.