Strings"

A few examples of the string helper functions and a comparison to substr and other R base functions.

First of all, we want the package loaded and attached. Same for the benchmarks.

library(Wmisc)

A string from (http://slipsum.com/).

s <- "You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man."

Head and Tail

demo

strHead(s)
strHeadLower(s)
strTail(s)

compare to built-in substr

substr(s,1,1)
tolower(substr(s,1,1))
substring(s,2)

benchmark

The benchmark results will vary greatly depending on the version of R used. With R 3.3 (upwards) the built-in functions should be preferred.

if (requireNamespace("DiagrammeR", quietly = TRUE)) { # only benchmark if microbenchmark is installed
  library(microbenchmark)

  microbenchmark(substr(s,1,1),strHead(s),times=100000)
  microbenchmark(tolower(substr(s,1,1)),strHeadLower(s),times=100000)
  microbenchmark(substring(s,2),strTail(s),times=100000)
} else {
  print("The benchmarks were skipped as package microbenchmark is not installed.")
} 

Take and Drop

substr(s,1,42)
strTake(s,42)
substring(s,43)
strDrop(s,42)
if (requireNamespace("DiagrammeR", quietly = TRUE)) { # only benchmark if microbenchmark is installed
  microbenchmark(substr(s,1,42),strTake(s,42),times=100000)
  microbenchmark(substring(s,43),strDrop(s,42),times=100000)
}  


Try the Wmisc package in your browser

Any scripts or data that you put into this service are public.

Wmisc documentation built on Jan. 14, 2018, 1:04 a.m.