library(tidyverse)
This section has the code for the rename() documentation.
tibble1 <- tribble(~A, ~b, ~c,
1, 2, 3,
4, 5, 6)
print(tibble1)
## # A tibble: 2 x 3
## A b c
## <dbl> <dbl> <dbl>
## 1 1 2 3
## 2 4 5 6
glimpse(tibble1)
## Rows: 2
## Columns: 3
## $ A <dbl> 1, 4
## $ b <dbl> 2, 5
## $ c <dbl> 3, 6
tibble1 %>% rename(a = A)
## # A tibble: 2 x 3
## a b c
## <dbl> <dbl> <dbl>
## 1 1 2 3
## 2 4 5 6
tibble1 %>% rename(B = b, C = c)
## # A tibble: 2 x 3
## A B C
## <dbl> <dbl> <dbl>
## 1 1 2 3
## 2 4 5 6
tibble2 <- tribble(~`flat rate`, ~`2cylinder`, ~`price_in_$`,
1, 2, 3,
4, 5, 6)
print(tibble2)
## # A tibble: 2 x 3
## `flat rate` `2cylinder` `price_in_$`
## <dbl> <dbl> <dbl>
## 1 1 2 3
## 2 4 5 6
glimpse(tibble2)
## Rows: 2
## Columns: 3
## $ `flat rate` <dbl> 1, 4
## $ `2cylinder` <dbl> 2, 5
## $ `price_in_$` <dbl> 3, 6
tibble2 %>%
rename(flat_rate = `flat rate`,
cylinder2 = `2cylinder`,
price_in_dollars = `price_in_$`)
## # A tibble: 2 x 3
## flat_rate cylinder2 price_in_dollars
## <dbl> <dbl> <dbl>
## 1 1 2 3
## 2 4 5 6
There is a variation called rename_with() that applies a renaming function to all the column names:
tibble1 %>% rename_with(str_to_upper)
## # A tibble: 2 x 3
## A B C
## <dbl> <dbl> <dbl>
## 1 1 2 3
## 2 4 5 6
#tibble1 %>% rename_with(str_to_lower)
#tibble1 %>% rename_with(str_to_title)
rename_with()#Using a base-R function
iris %>% tibble %>% rename_with(toupper) %>% colnames
## [1] "SEPAL.LENGTH" "SEPAL.WIDTH" "PETAL.LENGTH" "PETAL.WIDTH" "SPECIES"
We can pass expressions to rename_with() using placeholder notation in which an expression is preceded by a tilde and uses the period symbol to indicate the place for the argument.
tibble1 %>% rename_with(~str_to_upper(.) )
## # A tibble: 2 x 3
## A B C
## <dbl> <dbl> <dbl>
## 1 1 2 3
## 2 4 5 6
Note that str_to_title() doesnโt treat periods as blanks.
iris <- tibble(iris)
iris %>% rename_with(str_to_title) #Sepal.Length -> Sepal.length
## # A tibble: 150 x 5
## Sepal.length Sepal.width Petal.length Petal.width Species
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 8 5 3.4 1.5 0.2 setosa
## 9 4.4 2.9 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
## # ... with 140 more rows
Here is a complicated example that solves the problem of the str_to_title() function not recognizing underscores and periods as blanks.
iris %>%
rename_with(~str_replace(string = .,
pattern="[.]",
replacement= " ")) %>%
rename_with(~str_to_title(.)) %>%
rename_with(~str_replace(string = .,
pattern="[ ]",
replacement= "_")) %>%
colnames #See next section
## [1] "Sepal_Length" "Sepal_Width" "Petal_Length" "Petal_Width" "Species"
The colnames() function returns a character vector of column names.
colnames(mtcars)
## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb"
Changing the column names vector would change the column names of the original.
df <- iris
colnames(df)
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
colnames(df) <- str_replace(colnames(df),"[.]","_")
colnames(df)
## [1] "Sepal_Length" "Sepal_Width" "Petal_Length" "Petal_Width" "Species"
Consider the data frame
my_frame <- data.frame(F=1:3, G=1:3) #3 rows, 2 columns
In what important way do the following two lines of code differ in their effects on my_frame?
my_frame %>% rename(newF = F, newG = G)
colnames(my_frame) <- c("newF","newG")
Answer: the first line creates a copy of my_frame but leaves the column names of my_frame unchanged; while the second line changes the column names of my_frame.
The rename() function can change the column names of a tibble or a data frame, but it cannot change the column names of a matrix or 2D-table.
If you have used the iris data frame, then you might appreciate the following renaming:
my_iris <- iris %>% rename(sepal_length = Sepal.Length,
sepal_width = Sepal.Width,
petal_length = Petal.Length,
petal_width = Petal.Width,
species = Species)
colnames(my_iris)
## [1] "sepal_length" "sepal_width" "petal_length" "petal_width" "species"