Welcome to my Open Notebook

This is an Open Notebook with Selected Content - Delayed. All content is licenced with CC-BY. Find out more Here.

ONS-SCD.png

really-useful-r-upcase-string

Here is a really useful R snippet from http://stackoverflow.com/a/6364905 with a minor modification to allow differnt splits

Code:r-upcase-string

x <- c("The", "quick", "Brown", "fox/lazy dog")
 
simpleCap <- function(x, tosplit = " ") {
  s <- strsplit(x, tosplit)[[1]]
  paste(toupper(substring(s, 1,1)), substring(s, 2),
      sep="", collapse=tosplit)
}
sapply(x, simpleCap)
sapply(x, simpleCap, tosplit = "/")

Posted in  research methods


blog comments powered by Disqus