r create matrix from repeat loop output

645 views Asked by At

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
 }
1

There are 1 answers

0
josliber On BEST ANSWER

Basically you're asking, for each value n in some vector N, to compute the percentage of values in each column of T that exceed n.

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:

N <- c(0.1, 0.2)
do.call(rbind, lapply(N, function(n) c(n=n, 100*colMeans(T > n))))
#        n        A   B        C
# [1,] 0.1 66.66667 100 66.66667
# [2,] 0.2 33.33333 100 66.66667

For each value n in N, the call lapply(N, function(n) c(n=n, 100*colMeans(T > n))) computes a vector that indicates n as well as the percentage of values in each column of T that exceed n. Then do.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 the N vector in this case with:

N <- seq(.8, 0, -.05)
N <- N[log(N) >= -6]