[R] How to Change Variable Names

This post covers how to change variable names in R Studio.


    1. Preparation

    We will use rename() function in dplyr package, so install the package and load it.

    install.packages("dplyr") #installing dplyr
    library(dplyr) #loading dplyr
    

    Also, it is always wise to make a copy of the original data frame before changing the variable names.

    new df name <- original df name  #syntax structure
    df1 <- df_raw #example
    


    2. Changing Variable Names

    Now we use rename() function.
    Basically we are changing the data frame, but only the content, a variable in this case, so the name of the data frame does not have to be changed.
    The exmaple below means that I changed the name of the variable from 'sex' to 'gender' in a data frame named 'df1'.
    df name <- rename(df name, new v name = original v name) #syntax structure
    df1 <- rename(df1, gender = sex) #example
    

    (Thumbnail image: photo by Markus Spiske on Unsplash)

    Post a Comment

    0 Comments