I want to make a new matrix B from a previous matrix A, where the length of rows and columns are the same in B and every position corresponds to a ranking of A. In particular, for any x of a location [i,j] in A, I want to find how many values are greater than [i,j] (which sum(A>x), which I can find when x is discrete, but not for any x), followed by division by the total number of observations*variables in the matrix A. I think using the apply function would be able to create matrix B as I wish, but I'm having trouble finding a way to apply use of "sum" for each position (i.e., sum(A>x)/# of positions in A. I think I could use apply(A, c(1,2), FUN(X...)), but I do not know what function I can use. Thanks for any suggestions.
Short version: matrix((length(M) - rank(M))/length(M), nrow=nrow(M), ncol=ncol(M))
Long version:
length(M)
will give you the number of elements in the matrix.
length(M) - rank(M)
will give the number of elements greater than each element.
So you want (length(M) - rank(M)) / length(M)
but formatted into a matrix like M, so
matrix((length(M) - rank(M))/length(M), nrow=nrow(M), ncol=ncol(M))