I have a matrix (all_parameters) with 150 columns and 100.000 rows. The value for each data element in this matrix is "1". Now I would like to replace the values with "0" if the following conditions are true:
col name either 10, 14, 27 row name starts with "T1_"
I have the following loop which works fine:
T1_missing = c(10,14,27)
for(i in 1:ncol(all_parameters)) {
if (any(T1_missing == as.integer(colnames(all_parameters)[i]))) {
for(j in 1:nrow(all_parameters)) {
if(grepl("^T1_", rownames(all_parameters)[j])) {
all_parameters[j,i] <- 0
}
}
}
}
The problem is that the execution of the loops takes an extraordinary long time. I already tried to use the apply function however I was not able to make it work. Can anybody please show me how this could be solved using an apply function (or anything else that is superior and faster over a for-loop).
Thanks for your help!