Built with Tutify.io

    AIStudyLabs

    dplyr Rename() - To Change Column Name

    dplyr Rename() - To Change Column Name

    By using rename() and rename_with() functions from the dplyr/tidyverse package you can rename/change a single column name, multiple columns, rename by index, and rename all column names on R DataFrame.

    dplyr is an R packagethat provides a grammar of data manipulation and provides a most used set of verbs that helps data science analysts to solve the most common data manipulation. In order to use this, you have to install it first using install.packages('dplyr') and load it using library(dplyr).

    Alternatively, you also install and load the library tidyverse to use the rename() function to rename columns.

    <a id="h-dplyr-rename-syntax"></a>

    dplyr rename() Syntax

    Following is the syntax of rename() and rename_with() functions.

    
    # Syntax
    rename(.data, ...) 
    
    rename_with(.data, .fn, .cols = everything(), ...)
    

    Let’s create an R DataFrame,

    
    # Create dataframe
    df=data.frame(id=c(11,22,33,44,55),
        pages=c(32,45,33,22,56),
        name=c("spark","python","R","java","jsp"),
        chapters=c(76,86,11,15,7),
        price=c(144,553,321,567,890))
    
    #display the dataframe
    print(df)
    

    Output:

    
    # Output
      id pages   name chapters price
    1 11    32  spark       76   144
    2 22    45 python       86   553
    3 33    33      R       11   321
    4 44    22   java       15   567
    5 55    56    jsp        7   890
    

    Notice that the column names are: id, pages, name, chapters, and price.

    <a id="h-dplyr-rename-column"></a>

    dplyr Rename Column

    rename() function from dplyr takes a syntax rename(new_column_name = old_column_name) to modify the column from an old to a new name. The following example renames the column from id to c1.

    The %>% operator is used to load renamed column names into a data frame. When using the %>% operator, the functions that come after it will be applied to the data frame on its left side.

    
    # Load library
    library("dplyr")
    
    #Change the column name - id to c1
    df %>% 
        rename("c1" = "id")
    

    Note that the below example doesn’t change the column on the existing data frame. print() of this data frame results in unchanged data. To update assign this statement to the existing data frame.

    
    #Change the column name - id to c1
    df <- df %>% 
        rename("id" = "c1")
    

    Yields the below output.

    
    #Output
      c1 pages   name chapters price
    1 11    32  spark       76   144
    2 22    45 python       86   553
    3 33    33      R       11   321
    4 44    22   java       15   567
    5 55    56    jsp        7   890
    

    <a id="dplyr-rename-multiple-columns"></a>

    Rename Multiple Columns using dplyr

    Similarly, we can also use the dplyr function to rename multiple columns, here all you need is you should know your old column name and the new column name.

    
    # Rename multiple columns
    df <- df %>% 
               rename("c2" = "pages",
               "c5" = "price")
    print(my_dataframe)
    

    Yields the below output.

    
    #Output
      c1    c2   name chapters    c5
    1 11    32  spark       76   144
    2 22    45 python       86   553
    3 33    33      R       11   321
    4 44    22   java       15   567
    5 55    56    jsp        7   890
    

    <a id="h-dplyr-rename-column-by-index"></a>

    dplyr Rename Column by Index

    To rename column by index using the dplyr function, just use the index for old column names in the above example. This is the best approach as it is easily readable and the code is more organized. Note that dplyr doesn’t change the current data frame instead, it returns the new data frame after the rename, hence we need to assign the output of the rename to a variable.

    
    # Rename column by index
    df <- df %>% 
           rename(c1 = 1, c3 = 3)
    

    Yields the below output.

    
    #Output
      c1    c2     c3 chapters    c5
    1 11    32  spark       76   144
    2 22    45 python       86   553
    3 33    33      R       11   321
    4 44    22   java       15   567
    5 55    56    jsp        7   890
    

    <a id="h-dplyr-rename-all-columns"></a>

    dplyr Rename All Columns

    rename_with() function is from the R dplyr library that can be used to rename all data frame columns. In the below example let’s use this to convert all column names to upper case.

    
    # Rename using rename_with()
    my_dataframe <- my_dataframe %>%
      rename_with(toupper)
    

    Yields the below output.

    
    #Output
      C1    C2     C3 CHAPTERS    C5
    1 11    32  spark       76   144
    2 22    45 python       86   553
    3 33    33      R       11   321
    4 44    22   java       15   567
    5 55    56    jsp        7   890
    

    Conclusion

    In this article, you have learned the syntax of rename() and rename_with() functions from the dplyr package that is used to rename a column, rename multiple columns, rename by index, and rename all column names.

    Related Articles

    References