Mārtiņš Miglinieks - 6 months ago 34

R Question

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

`m`

`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`

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

Answer

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
```

Source (Stackoverflow)