Vectorizing

I will compare the performance of a function with a loop and a vectorized function

With loop

myFunc1 <- function(myString) {
  len<-nchar(myString)
  df <-data.frame(first=character(), rest=character())
  for (i in 1:(len-1)) {
    df<-rbind(df, data.frame(first=substr(myString, 1, i), rest=substr(myString, i+1,len)))
  }
  return(df)
}

myFunc1("hola")
##   first rest
## 1     h  ola
## 2    ho   la
## 3   hol    a

Vectorized

You can also embed plots, for example:

myFunc2 <- function(myString) {
  len<-nchar(myString)
  return(data.frame(first=substring(myString, 1, 1:(len-1)),
                    rest=substring(myString, 2:len, len)))
}
myFunc2("hola")
##   first rest
## 1     h  ola
## 2    ho   la
## 3   hol    a

Lets measure

We will use tictoc to measure

library("tictoc")

myStr<-"dsfwrsdfq4rsadfq4rasdfasfasfmyStrdsfwrsdfq4rsadfq4rasdfasfasfmyStrdsfwrsdfq4rsadfq4rasdfasfasfmyStr"

tic("start myFunc1 - with loop")
a<-myFunc1(myStr)
toc()
## start myFunc1 - with loop: 0.28 sec elapsed
tic("start myFunc2 - vectorized")
b<-myFunc2(myStr)
toc()
## start myFunc2 - vectorized: 0.03 sec elapsed

Vectorized function is ~10x faster for this example!