0

I am new to R, and looking for an apply type function to work with 2 arrays at once (simultaneous update).

For example, let's say I have some variables X and P:

X = array(rep(0, 10), dim=c(10, 1))
P = array(rep(1, 10), dim=c(10, 1))

which are governed by the system of equations:

X[k,] = 2*X[k-1]
P[k,] = 3*X[k] + X[k-1] + 3

Obviously, this can easily be accomplished with a for-loop, however, I have read/confirmed myself that for loops work horrendously for large inputs, and I wanted to start getting into good R coding practice, so I am wondering, what is the best way to do this in an apply-type logic? I am looking for something like,

sapply(2:dim(X)[1], function(k) {
    X[k,] = 2*X[k-1]
    P[k,] = 3*X[k] + X[k-1] + 3
})

But this obviously won't work, as it doesn't actually update X and P internally. Any tips/tricks for how to make my for-loops faster, and get in better R coding practice? Thanks in advance!

Rich
  • 71
  • 8
  • 1
    It appears all values of `X` will be 0, what am I missing ? – steveb Aug 19 '16 at 05:22
  • Many times, the assertion that `for` loops are *evil* is misplaced. What code and what benchmarks do you have to substantiate your claim that *"for loops work horrendously for large inputs"*? R has made great strides in the last few years in this arena, and though I'm a big fan in the `apply` family of functions *when it makes sense*, I also realize that sometimes the shoe-horn is a little too aggressive. – r2evans Aug 19 '16 at 05:32
  • do not use apply but vectorization, for example on `X`: `head(X,1)*(2^seq(nrow(X)))` – Colonel Beauvel Aug 19 '16 at 08:00

1 Answers1

1

You could do the following below. The <<- operator will set X and P outside of the function

sapply(2:dim(X)[1], function(k) {
    X[k,] <<- 2*X[k-1]
    P[k,] <<- 3*X[k] + X[k-1] + 3
})

As pointed out by thelatemail in the comments, using <<- can be problematic because of the side effects it can have. See the links below, the one comparing for loops (and other loops) to the apply family of functions.

Here is a link to documentation on assignment operators in R.

Here is a StackOverflow link on for loop vs. apply functions that talks about performance.

Community
  • 1
  • 1
steveb
  • 5,382
  • 2
  • 27
  • 36
  • 1
    This is exactly what I was looking for. Thanks! – Rich Aug 19 '16 at 06:13
  • Using `<<-` is just asking for trouble. Why not just use `<-` in an old school loop `for( k in 2:dim(X)[1]) {X[k,] <- 2*X[k-1]; P[k,] <- 3*X[k] + X[k-1] + 3}` ? I'd be amazed if it was any slower in real terms. – thelatemail Aug 19 '16 at 06:16
  • @thelatemail My answer was mainly focused on answering how one could update the variables outside of a somewhat simple `sapply` in a simple coding situation (that is what was being asked). However, as a general practice for a little more complex code, changing variables outside the scope of the function can be problematic (as you had mentioned). In other words, I agree it is good to avoid it. – steveb Aug 19 '16 at 06:23
  • @thelatemail FYI, I just added a link to a SO post which talks about "for loops" vs. the "apply" family of functions. – steveb Aug 19 '16 at 06:30