Take the 2-minute tour ×

I've created an 8 x 1000 matrix of Exp() variables. This represents 1000 iterations (columns) of sampling 8 times from an exponential distribution. I am trying to figure out how to get the percentage of values in each column that are less than a critical value. So I end up with a vector of 1000 percentages. I've tried a couple things but still being relatively new to R I'm having some difficulty.

This is my current version of code that doesn't quite work. I've used the apply function (without the for loop) when I want the mean or variance of the samples, so I've been trying this approach but this percentage thing seems to require something different. Any thoughts?




 for(i in 1:m){

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You can use apply without any for loop and get the answer:

percentages = apply(Mxs, 2, function(column) mean(column < crit))

Note the creation of an anonymous function with function(column) mean(column < crit). You probably used apply(Mxs, 2, mean) when you wanted the means of the columns, or apply(Mxs, 2, var) when you wanted the variance of the columns, but notice that you can put any function you want into that space and it will perform it on each column.

Also note that mean(column < crit) is a good way to get the percentage of values in column less than crit.

share|improve this answer
add comment

You can use colMeans:

[1] 0.500 0.750 0.250 0.375 0.375 0.875 ...
share|improve this answer
add comment

Your Answer


