R Cook Book
This page is dedicated to the little R functions that we write to do various things. Please feel free to add to the list or make improvements to current functions. If you do add (or change) something, please put your code into a (existing or new) category and include a small description that explains what your code does (for extra points include an example or two as well). Thanks!
String Manipulation
Splitting into segments
Here's a function to split a string into segments of length
n
. The default value of
n
is 80, since most terminals have rows that are 80 characters long.
strSegs <- function(x, n=80) {
retval <- c()
while(nchar(x) > n) {
segment <- substr(x, 1, n)
x <- substr(x, n+1, nchar(x))
retval <- c(retval, segment)
}
retval <- c(retval, x)
}
Example:
myString <- "This is a string that I want to split into segments."
segments <- strSegs(myString, 10)
cat(segments, sep="\n")
Data Frame Manipulation
Multisort
Here's a function that sorts each column of a data frame in decreasing or increasing order (the normal sort method only does either decreasing or increasing for
all columns). I took the idea for this function from
Cole Beck. You give the function your data frame and a series of name/value pairs, where each name is the name of a column, and each value is either
TRUE
or
FALSE
(
TRUE
for a decreasing sort and
FALSE
for an increasing sort.) The sort precedence goes from left to right. You do not have to specify all columns, but you must specify at least one.
multisort <- function(x, ...) {
# given the data frame x, and a list of name/value pairs, sort
pairList <- list(...)
# make sure names from ... are valid
tmp <- intersect(names(pairList), names(x))
if (length(tmp) = 0 || tmp
names(pairList)) {
stop("Invalid column names.")
}
retval <- x
for (i in length(pairList):1) {
name <- names(pairList[i])
if (pairListi = TRUE && pairListi = FALSE) {
# make sure values are TRUE or FALSE only
stop(paste("Invalid value (", pairListi, ") for ", name,
" (should be TRUE or FALSE).", sep=""))
}
retval <- retval[order(retval[,name], decreasing=pairListi),]
}
return(retval)
}
Example:
x <- data.frame(uno=sample(c("a","b","c"), 10, replace=TRUE),
dos=sample(c("x","y","z"), 10, replace=TRUE),
tres=sample(1:10, 10))
multisort(x, uno=TRUE, dos=FALSE)
multisort(x, dos=FALSE, uno=TRUE, tres=FALSE)