Mārtiņš Miglinieks - 1 year ago 66
R Question

# R: getting rid of for loop and speeding code

I would like to speed up my calculations and obtain results without using loop in function

`m`
. Reproducible example:

``````N <- 2500
n <- 500
r <- replicate(1000, sample(N, n))

m <- function(r, N) {
ic <- matrix(0, nrow = N, ncol = N)
for (i in 1:ncol(r)) {
p <- r[, i]
ic[p, p] <- ic[p, p] + 1
}
ic
}

system.time(ic <- m(r, N))
#  user  system elapsed
#  6.25    0.51    6.76
isSymmetric(ic)
# [1] TRUE
``````

In every iteration of
`for`
loop we are dealing with matrix not vector, so how this could be Vectorized?

@joel.wilson The purpose of this function is to calculate pairwise frequencies of elements. So afterwards we could estimate pairwise inclusion probabilities.

Vectorized approach

``````m1 <- function(r, N) {
ic <- matrix(0, nrow = N, ncol = N)
i <- rep(1:ncol(r), each=nrow(r))
ic[cbind(as.vector(r), i)] <- 1
tcrossprod(ic)
}

system.time(ic1 <- m1(r, N))
#   user  system elapsed
#   0.53    0.01    0.55

all.equal(ic, ic1)
# [1] TRUE
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download