Learn R Programming

sjmisc (version 2.7.7)

var_rename: Rename variables

Description

This function renames variables in a data frame, i.e. it renames the columns of the data frame.

Usage

var_rename(x, ...)

Arguments

x

A data frame.

...

Pairs of named vectors, where the name (lhs) equals the column name that should be renamed, and the value (rhs) is the new column name.

Value

x, with new column names for those variables specified in ....

Examples

Run this code
# NOT RUN {
dummy <- data.frame(
  a = sample(1:4, 10, replace = TRUE),
  b = sample(1:4, 10, replace = TRUE),
  c = sample(1:4, 10, replace = TRUE)
)

var_rename(dummy, a = "first.col", c = "3rd.col")

# using quasi-quotation
library(rlang)
v1 <- "first.col"
v2 <- "3rd.col"
var_rename(dummy, a = !!v1, c = !!v2)

x1 <- "a"
x2 <- "b"
var_rename(dummy, !!x1 := !!v1, !!x2 := !!v2)

# }

Run the code above in your browser using DataLab