Rename

The function dplyr::rename() allows you to rename column names in a tibble.

Tidyverse reference page

Dataset

For demonstration we’ll load the mammal_sleep_tbl data from the mgrtibbles package (hyperlink includes install instructions). For easier viewing we’ll subset it so it only has 5 rows.

#Load package
library("mgrtibbles")
#mammal_sleep_tbl tibble for demonstration
mammal_sleep_tbl<- mgrtibbles::mammal_sleep_tbl |> dplyr::slice(1:5)
mammal_sleep_tbl
# A tibble: 5 × 11
  species body_wt brain_wt non_dreaming dreaming total_sleep life_span gestation
  <chr>     <dbl>    <dbl>        <dbl>    <dbl>       <dbl>     <dbl>     <dbl>
1 Africa… 6654      5.71           NA       NA           3.3      38.6       645
2 Africa…    1      0.0066          6.3      2           8.3       4.5        42
3 Arctic…    3.38   0.0445         NA       NA          12.5      14          60
4 Arctic…    0.92   0.0057         NA       NA          16.5      NA          25
5 Asiane… 2547      4.60            2.1      1.8         3.9      69         624
# ℹ 3 more variables: predation <fct>, exposure <fct>, danger <fct>

Rename columns

The format of the rename() function is:

dplyr(new_name=old_name)

Rename column total_sleep to tot_sleep.

mammal_sleep_tbl |> dplyr::rename(tot_sleep=total_sleep)
# A tibble: 5 × 11
  species   body_wt brain_wt non_dreaming dreaming tot_sleep life_span gestation
  <chr>       <dbl>    <dbl>        <dbl>    <dbl>     <dbl>     <dbl>     <dbl>
1 Africane… 6654      5.71           NA       NA         3.3      38.6       645
2 Africang…    1      0.0066          6.3      2         8.3       4.5        42
3 ArcticFox    3.38   0.0445         NA       NA        12.5      14          60
4 Arcticgr…    0.92   0.0057         NA       NA        16.5      NA          25
5 Asianele… 2547      4.60            2.1      1.8       3.9      69         624
# ℹ 3 more variables: predation <fct>, exposure <fct>, danger <fct>

Rename columns body_wt and brain_wt to body_kg and brain_kg.

mammal_sleep_tbl |> dplyr::rename(body_kg=body_wt, brain_kg=brain_wt)
# A tibble: 5 × 11
  species body_kg brain_kg non_dreaming dreaming total_sleep life_span gestation
  <chr>     <dbl>    <dbl>        <dbl>    <dbl>       <dbl>     <dbl>     <dbl>
1 Africa… 6654      5.71           NA       NA           3.3      38.6       645
2 Africa…    1      0.0066          6.3      2           8.3       4.5        42
3 Arctic…    3.38   0.0445         NA       NA          12.5      14          60
4 Arctic…    0.92   0.0057         NA       NA          16.5      NA          25
5 Asiane… 2547      4.60            2.1      1.8         3.9      69         624
# ℹ 3 more variables: predation <fct>, exposure <fct>, danger <fct>

Names with spaces

Column names with spaces, numbers, or special characters must be flanked with quotes. The following quotes can be used:

  • `: Backtick (recommended)
  • ": Double quote
  • ': Single quote

Rename columns with spaces in mgrtibble::crab_age_pred_tbl to replace the space with an underscore (_).

Note: The use of multiple types of quotes is for demonstrative purposes and it is recommended you use backticks (`).

mgrtibbles::crab_age_pred_tbl |> 
    #Slice top 5 rows
    dplyr::slice(1:5) |>
    dplyr::rename(
        shucked_weight=`Shucked Weight`,
        viscera_weight= "Viscera Weight",
        shell_weight='Shell Weight'
    )
# A tibble: 5 × 9
  Sex   Length Diameter Height Weight shucked_weight viscera_weight shell_weight
  <fct>  <dbl>    <dbl>  <dbl>  <dbl>          <dbl>          <dbl>        <dbl>
1 F      1.44     1.18   0.412  24.6           12.3            5.58         6.75
2 M      0.888    0.65   0.212   5.40           2.30           1.37         1.56
3 I      1.04     0.775  0.25    7.95           3.23           1.60         2.76
4 F      1.18     0.888  0.25   13.5            4.75           2.28         5.24
5 I      0.888    0.662  0.212   6.90           3.46           1.49         1.70
# ℹ 1 more variable: Age <dbl>