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)
library(microbenchmark)
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."
strHead(s)
## Warning in strHead(s): '.Random.seed' ist kein Integer-Vektor sondern vom
## Typ 'NULL', wird also ignoriert
## [1] "Y"
strHeadLower(s)
## [1] "y"
strTail(s)
## [1] "ou 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."
substr(s,1,1)
## [1] "Y"
tolower(substr(s,1,1))
## [1] "y"
substring(s,2)
## [1] "ou 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."
The benchmark results will vary greatly depending on the version of R used. With R 3.3 the built-in functions should be preferred.
microbenchmark(substr(s,1,1),strHead(s),times=100000)
## Unit: microseconds
## expr min lq mean median uq max neval
## substr(s, 1, 1) 1.182 1.576 2.366562 1.970 1.970 4091.953 1e+05
## strHead(s) 2.757 3.545 6.296062 3.546 3.939 71205.886 1e+05
microbenchmark(tolower(substr(s,1,1)),strHeadLower(s),times=100000)
## Unit: microseconds
## expr min lq mean median uq max neval
## tolower(substr(s, 1, 1)) 2.757 3.152 3.985980 3.545 3.546 3570.121 1e+05
## strHeadLower(s) 3.151 3.546 5.204711 3.939 3.940 8561.595 1e+05
microbenchmark(substring(s,2),strTail(s),times=100000)
## Unit: microseconds
## expr min lq mean median uq max neval
## substring(s, 2) 3.939 4.726 5.547589 4.727 5.121 3553.186 1e+05
## strTail(s) 4.726 5.121 6.961328 5.514 5.515 6540.036 1e+05
substr(s,1,42)
## [1] "You think water moves fast? You should see"
strTake(s,42)
## [1] "You think water moves fast? You should see"
substring(s,43)
## [1] " 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."
strDrop(s,42)
## [1] " 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."
microbenchmark(substr(s,1,42),strTake(s,42),times=100000)
## Unit: microseconds
## expr min lq mean median uq max neval
## substr(s, 1, 42) 1.576 1.970 2.377756 1.970 2.364 3259.385 1e+05
## strTake(s, 42) 3.545 4.333 5.894722 4.333 4.333 6517.587 1e+05
microbenchmark(substring(s,43),strDrop(s,42),times=100000)
## Unit: microseconds
## expr min lq mean median uq max neval
## substring(s, 43) 3.939 4.333 5.467420 4.727 4.727 3199.916 1e+05
## strDrop(s, 42) 4.726 5.514 7.680432 5.908 5.909 7598.273 1e+05