1

I'm a beginner with R and I have written this simple loop:

for(i in 1:12000){
    if(v$piano.tariff[i] == 2) {v$piano.tariff[i] = 0}
    else {v$piano.tariff[i] = 1}
}

Where v is a data frame and piano.tariff one of its columns. What the loop does is simply change each value of the piano.tariff column to either 1 or 0, from their initiali values of 2 and 5.

Now, the code works, but the problem is that it is ridiculously slow. It takes up to 4-5 minutes to complete! In C++ o C# such a loop would barely require a few seconds.

Why is this so slow? Is there a faster way to implement this? Or is it simply that R is slow, and that's it?

Master_T
  • 7,232
  • 11
  • 72
  • 144
  • If you're newer to R a lot of these questions regarding loops and vectorization are answered in: http://www.burns-stat.com/pages/Tutor/R_inferno.pdf – Tyler Rinker Apr 08 '13 at 16:59

3 Answers3

7

You may want to use ifelse instead which is a vectorized R function, it'll be faster

ifelse(v$piano.tariff==2, 0, 1)

Since you didn't provide a reproducible example I couldn't benchmark the performace.

Community
  • 1
  • 1
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
5

I think you could try a vectorized approach here.

EDIT Thanks to Henrik, the previous version was a bit off. I think the new approach is OK.

twos <- v$piano.tariff == 2
notwos <- v$piano.tariff != 2
v[twos, "piano.tariff"] <- 0
v[notwos, "piano.tariff"] <- 1

Fast enough for you? :)

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • It's instantaneous, thanks!! Tho I modified it slightly, since I knew the other value was 5 I used a "==" in the second line, otherwise as Henrik points out, it wouldn't work properly. – Master_T Apr 08 '13 at 09:36
  • @Master_T see my edit. Henrik noted that my first approach was wrong. – Roman Luštrik Apr 08 '13 at 09:40
1

In general you need to be careful about loops in R, but more particularly assignments inside loops like v$piano.tariff[i] = v$piano.tariff[i]+1 which cause the entire vector to be re-allocated.

Refer to Patrick Burns online book on these sorts of issues The R Inferno for a lot more detail on this.

Sean
  • 3,765
  • 3
  • 26
  • 48