For each value n
in some vector N
, I want to compute the percentage of values exceed n
for each variable in my data frame T
.
Consider the following input data frame:
T <- data.frame(A=c(0.1,0.2,0.3), B=c(0.3,0.3,0.9),C=c(1,0.5,0))
T
# A B C
# 1 0.1 0.3 1.0
# 2 0.2 0.3 0.5
# 3 0.3 0.9 0.0
I would like the output to be a matrix that looks something like this:
A B C
n=0.1 66.6 100 66.6
n=0.2 33.3 100 66.6
My current implementation is not working:
n <- 0.8
repeat {
Tlogic <- T > n
TU <- as.matrix(apply(Tlogic,2,sum))
q = NULL
for (i in seq(along=TU[,1]))
{
percent <- (TU[i]/nrow(T))*100
q = c(q, percent)
}
n <- n - 0.05;
print(n);
if(log(n) < -6) break
}
Basically you're asking, for each value
n
in some vectorN
, to compute the percentage of values in each column ofT
that exceedn
.You can actually do this in one line in R by moving from a solution that writes out loops to one that uses the
*apply
functions in R:For each value
n
inN
, the calllapply(N, function(n) c(n=n, 100*colMeans(T > n)))
computes a vector that indicatesn
as well as the percentage of values in each column ofT
that exceedn
. Thendo.call(rbind, ...)
groups all of these together into a final output matrix.In your case, you want N to form a decreasing sequence (by 0.05 each step) from 0.8 until
log(n) < -6
. You can get theN
vector in this case with: