Learn R Programming

omnibus (version 1.2.15)

renameCol: Rename columns

Description

Rename columns of a data.frame or matrix.

Usage

renameCol(x, old, new)

Value

A data.frame or matrix.

Arguments

x

A data.frame or matrix.

old

Character vector with names(s), or numeric vector of the indices of the column(s) you want to rename.

new

Character vector of new names.

Examples

Run this code

x <- data.frame(old_x = 1:5, old_y = letters[1:5], old_z = LETTERS[1:5])
x
renameCol(x, c('old_y', 'old_z'), c('new_Y', 'new_Z'))
renameCol(x, c(2, 3), c('new_Y', 'new_Z')) # same as above

# Long way:
new <- c('new_Y', 'new_Z')
colnames(x)[match(c('old_y', 'old_z'), colnames(x))] <- new

Run the code above in your browser using DataLab