Learn R Programming

SGP (version 1.5-0.0)

capwords: Function for converting all caps to mixed case. Useful in data cleaning.

Description

The function capwords converts characters to mixed case character as intelligently as possible and leading/trailing spaces.

Usage

capwords(x, special.words = c("ELA","I", "II", "III", "IV", "EMH", "HS", "MS", "ES", "SES", "IEP", "ELL", "MAD", "SD", "SWD", "US", "SGP", "SIMEX"))

Arguments

x
A character string to be converted to mixed case.
special.words
A character vector (see default above), specifying words to not convert to mixed case.

Value

Examples

Run this code
capwords("TEST") ## Test
capwords("TEST1 TEST2") ## Test1 Test2
capwords("O'NEIL") ## O'Neil
capwords("JOHN'S") ## John's

## Use sapply for converting character vectors

test.vector <- paste("TEST", 1:10, sep="")
sapply(test.vector, capwords)


## With factors, convert levels instead of the entire vector

test.factor <- factor(paste("TEST", rep(letters[1:10], each=50)))
levels(test.factor) <- sapply(levels(test.factor), capwords)
levels(test.factor)

Run the code above in your browser using DataLab