Rich - 1 year ago 47

R Question

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!

Answer Source

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.