Learn R Programming

rccmisc (version 0.3.7)

change_col_name: Change one column name of a data.frame

Description

A data.frame can, by definition, have non unique column names. To change a column name to a new name that is already present in the data.frame might therefore lead to undesiered results. This function handles this problem by dropping the original column cousing the name clash.

Usage

change_col_name(x, old_name, new_name, warning = FALSE)

Arguments

x
data.frame with a column name to change
old_name
name (as character string) of column in x that should be changed.
new_name
new name (as character string) to use instead of old_name
warning
should a warning be given if a name clash occours (FALSE by default)

Value

The same data.frame but with one column named changed. (Note that the output might have one column less if dropped after name clash.)

Examples

Run this code
ab <- ba <- data.frame(a = letters[1:10], b = 1:10)

# One "traditional" way to change a column name
names(ab)[names(ab) == "a"] <- "b"
names(ab)
ab$b # Returns the first column with name "b"

# Using change_col_names instead:
change_col_name(ba, "a", "b")

Run the code above in your browser using DataLab