rename variable in r dplyr

To rename a variable in R using the dplyr package, you can use the rename() function. Here are the steps to rename a variable in R using dplyr:

  1. Load the dplyr package:

This step is necessary to access the rename() function.

  1. Specify the data frame and the variable(s) you want to rename:
new_df <- rename(old_df, new_variable_name = old_variable_name)

In this step, old_df is the original data frame, and new_variable_name is the new name you want to assign to the variable. old_variable_name is the current name of the variable you want to rename.

  1. Assign the result to a new data frame:
new_df <- rename(old_df, new_variable_name = old_variable_name)

This step creates a new data frame new_df with the renamed variable.

Here's an example to illustrate the steps:


# Create a sample data frame
df <- data.frame(age = c(25, 30, 35),
                 height = c(160, 170, 180))

# Rename the variable "age" to "new_age"
new_df <- rename(df, new_age = age)

# Print the new data frame


  new_age height
1      25    160
2      30    170
3      35    180

In this example, the variable "age" in the original data frame df is renamed to "new_age" in the new data frame new_df.