### Name: chartr ### Title: Character Translation and Casefolding ### Aliases: chartr tolower toupper casefold ### Keywords: character ### ** Examples x <- "MiXeD cAsE 123" chartr("iXs", "why", x) chartr("a-cX", "D-Fw", x) tolower(x) toupper(x) ## "Mixed Case" Capitalizing - toupper( every first letter of a word ) : .simpleCap <- function(x) { s <- strsplit(x, " ")[[1]] paste(toupper(substring(s, 1,1)), substring(s, 2), sep="", collapse=" ") } .simpleCap("the quick red fox jumps over the lazy brown dog") ## -> [1] "The Quick Red Fox Jumps Over The Lazy Brown Dog" ## and the better, more sophisticated version: capwords <- function(s, strict = FALSE) { cap <- function(s) paste(toupper(substring(s,1,1)), {s <- substring(s,2); if(strict) tolower(s) else s}, sep = "", collapse = " " ) sapply(strsplit(s, split = " "), cap, USE.NAMES = !is.null(names(s))) } capwords(c("using AIC for model selection")) ## -> [1] "Using AIC For Model Selection" capwords(c("using AIC", "for MODEL selection"), strict=TRUE) ## -> [1] "Using Aic" "For Model Selection" ## ^^^ ^^^^^ ## 'bad' 'good' ## -- Very simple insecure crypto -- rot <- function(ch, k = 13) { p0 <- function(...) paste(c(...), collapse="") A <- c(letters, LETTERS, " '") I <- seq_len(k); chartr(p0(A), p0(c(A[-I], A[I])), ch) } pw <- "my secret pass phrase" (crypw <- rot(pw, 13)) #-> you can send this off ## now ``decrypt'' : rot(crypw, 54 - 13)# -> the original: stopifnot(identical(pw, rot(crypw, 54 - 13)))