I have a question pertaining to R.
I have some sequentially numbered matrices (all of the same dimensions) and I want to search them all and produce a final matrix that contains (for each matrix element) the number of times a defined threshold was exceeded.
As an example, I could choose a threshold of 0.7 and I could have the following three matrices.
matrix1 [,1] [,2] [,3] [1,] 0.38 0.72 0.15 [2,] 0.58 0.37 0.09 [3,] 0.27 0.55 0.22 matrix2 [,1] [,2] [,3] [1,] 0.19 0.78 0.72 [2,] 0.98 0.65 0.46 [3,] 0.72 0.57 0.76 matrix3 [,1] [,2] [,3] [1,] 0.39 0.68 0.31 [2,] 0.40 0.05 0.92 [3,] 1.00 0.43 0.21
My desired output would then be
[,1] [,2] [,3] [1,] 0 2 1 [2,] 1 0 1 [3,] 2 0 1
If I do this:
test <- matrix1 >= 0.7
test[test==TRUE] = 1
then I get a matrix that has a 1 where the threshold is exceeded, and 0 where it's not. So this is a key step in what I want to do:
test= [,1] [,2] [,3] [1,] 0 1 0 [2,] 0 0 0 [3,] 0 0 0
My thought is to make a loop so I perform this calculation on each matrix and add each result of "test" so I get the final matrix I desire. But I'm not sure about two things: how to use a counter in the variable name "matrix", and second if there's a more efficient way than using a loop.
So I'm thinking of something like this:
output = matrix(0,3,3)
for i in 1:3 {
test <- matrixi >= 0.7
test[test==TRUE] = 1
output = output + test }
Of course, this doesn't work because matrixi does not translate to matrix1, matrix2, etc.
I really appreciate your help!!!