0

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.

Josh
  • 1,210
  • 12
  • 30

1 Answers1

1

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))))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213