7.2 rename()

What it does: Renames a column/variable.

Renames the price variable as PRICE (notice that the new label goes first in the argument). Again, it’s important to remember that there are many different ways to perform the same task. The method each programmer chooses is all about personal preference (or current knowledge of available functions).

diamonds %>% rename(PRICE = price)

# is the same as

diamonds %>% 
  mutate(PRICE = price) %>% # creates new variable based on old variable
  select(-price) # removes old variable from dataset

Renames the x, y, z variables to length, width, and depth (see ?diamonds):

diamonds %>% rename(length = x, width = y, depth = z)

# is the same as

diamonds %>% 
  mutate(length = x, width = y, depth = z) %>% 
  select(-x, -y, -z)