Learn R Programming

colr (version 0.1.900)

csub: a function to change column names

Description

a function to change column names

Usage

csub(x, pattern, replacement, dim = c("c", "r"), gl = TRUE)

Arguments

x
A dataframe, list or matrix with column names
pattern
A string
replacement
A string
dim
A character either "c" for selection of columns or "r" for selection of rows, if x is a list this parameter has no meaning
gl
A boolean, if TRUE (the default) all occurences in the input strings (row or column names) are replaced, if FALSE only the first occurence in every string is replaced.

Value

A dataframe, list or matrix where the columns or rows are renamed based on the 'Regex' substitution defined in the pattern and the replacement. By default column are renamed, unless the dim parameter specifies "r" for rows. The default is to use gsub (replacing all occurences of pattern in each column name), to replace only the first occurence set gl to FALSE.For lists the function acts on the highest level of the list and this is irrespective of the gl parameter.

See Also

regex{base}

Examples

Run this code
head(csub(iris, "\\.", "-")) # will change all dots in column names in "-"
head(csub(iris, "[pP]etal", "Beetle"))

# a more complex example showing the power of 'regex'

head(csub(data.frame(WorldPhones),"^([NM](?:\\w{2})?\\.)Amer", "\\1America"))


# this example will also work on other types (the call data.frame is not necessary)

Run the code above in your browser using DataLab