I have a large (of course) matrix of spectroscopic data, with each column representing a different mass value, and the rows representing samples from the analysis. A small example...
mydata <- matrix(c(c(1.95,6,1,0),c(1.76,3,2,14),c(3.67,2,1.55,7),c(0.57,3,8,12),c(2.33,3,16,2)),nrow = 4, ncol = 5)
rnames <- c("threshold", "S1", "S2", "S3")
row.names(mydata)<- rnames
# [,1] [,2] [,3] [,4] [,5]
# threshold 1.95 1.76 3.67 0.57 2.33
# S1 6.00 3.00 2.00 3.00 3.00
# S2 1.00 2.00 1.55 8.00 16.00
# S3 0.00 14.00 7.00 12.00 2.00
The first row represents a threshold value, and to be considered, the sample value must be 3x the threshold. I want to compare the first row value against all values in the subsequent rows within the column, and return the cell value if it is =>3x the first row value, and replace the cell with a "0" otherwise.
So, for that small sample data, the output matrix I would hope to achieve would look like:
mydata2 <- matrix(c(c(1.95,6,0,0),c(1.76,0,0,14),c(3.67,0,0,0),c(0.57,3,8,12),c(2.33,0,16,0)),nrow = 4, ncol = 5)
row.names(mydata2) <- rnames
# [,1] [,2] [,3] [,4] [,5]
# threshold 1.95 1.76 3.67 0.57 2.33
# S1 6.00 0.00 0.00 3.00 0.00
# S2 0.00 0.00 0.00 8.00 16.00
# S3 0.00 14.00 0.00 12.00 0.00
I'm thinking there is a way to use apply to run this, but my knowledge of R does not extend that far (yet).
I should note that the threshold (first) row was initially a separate 1xn matrix, which was inserted into the first row using InsertRow. If it would be easier to compare the data matrix against a 'threshold' matrix, rather than comparing rows intra-matrix, all the better.
Thank you for your help in solving this!