This function renames variables in a data frame, i.e. it renames the columns of the data frame.
var_rename(x, ..., verbose = TRUE)
rename_variables(x, ..., verbose = TRUE)
rename_columns(x, ..., verbose = TRUE)x, with new column names for those variables specified in ....
dummy <- data.frame(
a = sample(1:4, 10, replace = TRUE),
b = sample(1:4, 10, replace = TRUE),
c = sample(1:4, 10, replace = TRUE)
)
rename_variables(dummy, a = "first.col", c = "3rd.col")
#> first.col b 3rd.col
#> 1 3 1 3
#> 2 4 2 2
#> 3 2 1 4
#> 4 1 1 3
#> 5 2 3 4
#> 6 1 1 4
#> 7 1 2 2
#> 8 4 1 2
#> 9 4 3 3
#> 10 3 1 4
# using quasi-quotation
library(rlang)
#>
#> Attaching package: ‘rlang’
#> The following objects are masked from ‘package:purrr’:
#>
#> %@%, flatten, flatten_chr, flatten_dbl, flatten_int, flatten_lgl,
#> flatten_raw, invoke, splice
#> The following objects are masked from ‘package:sjlabelled’:
#>
#> as_character, as_label
#> The following object is masked from ‘package:sjmisc’:
#>
#> is_empty
v1 <- "first.col"
v2 <- "3rd.col"
rename_variables(dummy, a = !!v1, c = !!v2)
#> first.col b 3rd.col
#> 1 3 1 3
#> 2 4 2 2
#> 3 2 1 4
#> 4 1 1 3
#> 5 2 3 4
#> 6 1 1 4
#> 7 1 2 2
#> 8 4 1 2
#> 9 4 3 3
#> 10 3 1 4
x1 <- "a"
x2 <- "b"
rename_variables(dummy, !!x1 := !!v1, !!x2 := !!v2)
#> first.col 3rd.col c
#> 1 3 1 3
#> 2 4 2 2
#> 3 2 1 4
#> 4 1 1 3
#> 5 2 3 4
#> 6 1 1 4
#> 7 1 2 2
#> 8 4 1 2
#> 9 4 3 3
#> 10 3 1 4
# using a named vector
new_names <- c(a = "first.col", c = "3rd.col")
rename_variables(dummy, new_names)
#> Error in eval(.dots[[1]]): object 'new_names' not found