user86533
user86533

Reputation: 333

R - efficiently loop through matrix

I have a matrix (named "my_matrix") with 150 columns (colnames are 1,2,3,4,5,6,7,...) and 100000 rows.

I perform the following operation:

missing = c(50,57,60,77,99,101,102,109)

for(i in 1:ncol(my_matrix)) {
  if(colnames(my_matrix)[i] %in% missing) {
    for(j in 1:nrow(my_matrix)) {
        if(grepl('_old$', rownames(my_matrix)[j])){
          my_matrix[j,i] <- my_matrix[gsub("_old", "_new", rownames(my_matrix)[j]),i]
        }
    }
  }
}

This operation works fine, and basically looks if the name of the column is found in "missing". If so, it looks whether the name of the row ends with "_old". If so, the value of the cell is replaced with the value of another cell (e.g. the value of column 50 & row 237478_old is replaced with the value of column 50 & row 237478_new and so on).

Unfortunately it is very slow and takes hours until the execution is finished. Is there any faster way to perform this operation (e.g. with apply)?

Thanks in advance!

Upvotes: 1

Views: 4806

Answers (1)

akrun
akrun

Reputation: 886988

We create the index of the columns ('j1') and the rows (for both the 'new' and 'old' row names), and extract the values with row/column indexing and replace with the values from the "new" row/column values.

 j1 <- colnames(my_matrix) %in% missing
 i1 <- grepl('_old$', rownames(my_matrix))
 i2 <- grepl('_new$', rownames(my_matrix))
 my_matrix[i1,j1] <- my_matrix(i2, j1]

Upvotes: 3

Related Questions