change_col_name: Change one column name of a data.frame

Description Usage Arguments Value Examples

View source: R/change_col_names.R

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

1
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

1
2
3
4
5
6
7
8
9
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")

rccmisc documentation built on May 2, 2019, 2:48 p.m.