Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R function with FOR loop on a vector

Surely a facepalm question, sorry. I have been trying to RTFM and google that but no luck.

I am trying to make a silly function in R that takes a vector as argument, loops through it with a for loop and does a very simple addition to each component.

func1 <- function(vector) {
  vr <- c() ### an empty vector 

  for (i in 1:length(vector)) { 
    vr <- vector[i]+i
  }
}

To me this looks correct, but I am always getting a NULL vector. Why?

like image 731
runlevel0 Avatar asked Feb 04 '15 16:02

runlevel0


1 Answers

You could write that much more efficiently like this:

func1 <- function(vector){
    vector + seq_along(vector)
}

This way you are taking advantage of vector addition.

For the sake of completeness, let's benchmark these two solutions. Here is the general loop solution:

func2 <- function(vector) {
    vr <- c() ### an empty vector 

    for (i in 1:length(vector)) { 
        vr[i] <- vector[i]+i
    }
    vr
}

As a step up, there is also the in-place solution

func3 <- function(vector) {    
    for (i in 1:length(vector)) { 
        vector[i] <- vector[i]+i
    }
    vector
}

Using microbenchmark we can see that the vectorized solution is by far the most efficient.

vec <- sample(100000, 10000)

library(microbenchmark)
microbenchmark(func1(vec), func3(vec), func2(vec))

Unit: microseconds
       expr       min        lq         mean    median       uq         max neval cld
 func1(vec)    29.998    36.984     44.78312    42.736    44.38     399.006   100  a 
 func3(vec) 12845.823 13666.432  14452.02863 14060.712 14708.53   25025.950   100  a 
 func2(vec) 84898.055 87354.750 110046.26659 88634.566 91193.38 1042819.269   100   b
like image 175
cdeterman Avatar answered Nov 12 '22 02:11

cdeterman