user3821273
user3821273

Reputation: 151

r - Fast way to loop through matrix and compute and assign a value to each element?

I have a large matrix of probabilities (call it A), N by 806, where N is typically a number in the thousands.

Using this matrix of probabilities, I want to create another matrix (call it B), N by 806, that contains only binary values. The value in B[i,j] is determined by using the corresponding probability in A[i,j] via binomial. The code I am using is below:

diCases <- matrix(0, nrow = numcases, ncol = numdis)
diConts <- matrix(0, nrow = numconts, ncol = numdis)

for(row in 1:nrow(diCases)) {
    print(paste('Generating disease profile for case', row, '...'))
    for(col in 1:ncol(diCases)) {
        pDis <- Pcases[row, col]
        diCases[row, col] <- rbinom(1, 1, pDis)
    }
}

for(row in 1:nrow(diConts)) {
    print(paste('Generating disease profile for control', row, '...'))
    for(col in 1:ncol(diConts)) {
        pDis <- Pconts[row, col]
        diConts[row, col] <- rbinom(1, 1, pDis)
    }
}

Basically, I have resorted to using nested for loops, looping through every column in each row and moving on to the next row, assigning a 1 or 0 based on the result of:

rbinom(1, 1, pDis)

where pDis is the A[i,j] mentioned in the beginning. As you can imagine, this is pretty slow and is the main bottleneck in my code. This block of code is in a simulation that I had planned to run over and over again, ideally in a short period of time.

Is there a faster way to accomplish this? I looked into the "apply" functions but couldn't really figure out how to make it work for this particular task.

Thank you all in advance.

Upvotes: 2

Views: 11266

Answers (1)

konvas
konvas

Reputation: 14366

Try

f <- function(prob.mat) 
    matrix(rbinom(prob.mat, 1, prob.mat), ncol = ncol(prob.mat))

diCases <- f(Pcases)
diConts <- f(Pconts)

Upvotes: 4

Related Questions