R: Change one column name of a data.frame

change_col_name {rccmisc} | R Documentation

## 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

    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")

[Package rccmisc version 0.3.7 Index]