R - efficiently loop through matrix -


i have matrix (named "my_matrix") 150 columns (colnames 1,2,3,4,5,6,7,...) , 100000 rows.

i perform 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, , looks if name of column found in "missing". if so, looks whether name of row ends "_old". if so, value of cell replaced value of cell (e.g. value of column 50 & row 237478_old replaced value of column 50 & row 237478_new , on).

unfortunately slow , takes hours until execution finished. there faster way perform operation (e.g. apply)?

thanks in advance!

we create index of columns ('j1') , rows (for both 'new' , 'old' row names), , extract values row/column indexing , replace values "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] 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -