Loops in R - Need to use index, anyway to avoid 'for'?
I know it's not the best practice in R to use the `for` loop because it doesn't have an enhanced performance. For almost all cases there is a function of the family `*apply` that solves our problems.

However I'm facing a situation where I don't see a workaround.

I need to calculate percent variation for consecutive values:

    pv[1] <- 0
    for(i in 2:length(x)) {
      pv[i] <- (x[i] - x[i-1])/x[i-1]
    }

So, as you can see, I have to use both the `x[i]` element, but also  the `x[i-1]` element. By using the `*apply` functions, I just see how to use the `x[i]`. Is there anyway I can avoid the `for`loops?
Loops in R - Need to use index, anyway to avoid 'for'?
===

I know it's not the best practice in R to use the `for` loop because it doesn't have an enhanced performance. For almost all cases there is a function of the family `*apply` that solves our problems.

However I'm facing a situation where I don't see a workaround.

I need to calculate percent variation for consecutive values:

    pv[1] <- 0
    for(i in 2:length(x)) {
      pv[i] <- (x[i] - x[i-1])/x[i-1]
    }

So, as you can see, I have to use both the `x[i]` element, but also  the `x[i-1]` element. By using the `*apply` functions, I just see how to use the `x[i]`. Is there anyway I can avoid the `for`loops?