Josh
Josh

Reputation: 1337

Count values above a range of thresholds

I have a data frame of values and want to count the number of values in each column that are greater than or equal to a series of thresholds. The ultimate goal is to produce a graph that looks like panels B and D in this figure graphs

I have figured out a way to do this with a for loop but would like to avoid a loop if possible:

data <- as.data.frame(attitude)
max <- 100
counts <- data.frame(t(c(score = 1, colSums(data >= 1))))
for (x in 2:max) {
  counts <- rbind(counts, c(score = x, colSums(data >= x)))
}

I tried adapting the code from this question (Count the number of values per column above a range of thresholds in R), but it gives an error and does not produce the expected result:

  as.data.frame() %>% 
  lapply(
    function(z) table(findInterval(z, 0:max, rightmost.closed = TRUE))
  ) %>% 
  do.call(cbind, .) %>%
  as.data.frame()

Is there a way to do this without a loop? Thanks.

Upvotes: 0

Views: 359

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389055

You can do this with sapply/lapply :

data <- as.data.frame(attitude)
num <- seq_len(100)

result <- data.frame(score = num, 
                     do.call(rbind, lapply(num, function(x) colSums(data >= x))))

Upvotes: 1

Related Questions