Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

r – Create a special counting table with variable thresholds

I have a dataframe named "data", as below :

id quantity
01 5
02 3
03 7
04 4
05 9

and I would like to set thresholds and count how many ids are equal or below that threshold, which means a dataframe "results" like :

threshold count
1 0
2 0
3 1
4 2
5 3
6 3
7 4
8 4
9 5
10 5

The only way I found to do this is to use "for" :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for(i in 1:10) {results$count[i] <- nrow(data[data$quantity <= i,]}

This instruction does work. However, with my real data, there are 500 thresholds and I have to repeat quite the same process 12 times… thus the "for" loop is very long to proceed. I couldn’t find something to replace that, I would rather something like :

results$count <- nrow(data[data$quantity <= results$threshold,]

but it doesn’t work ("longer object length is not a multiple of shorter object length").
Do you have some ideas?

>Solution :

Try this:

library(tidyverse)

df <- tribble(
  ~id, ~quantity,
  "01", 5,
  "02", 3,
  "03", 7,
  "04", 4,
  "05", 9
)

result <- map_dfr(1:10, function(x){
  tibble(
    threshold = x,
    count = sum(df$quantity <= x)
    )
})

result
#> # A tibble: 10 × 2
#>    threshold count
#>        <int> <int>
#>  1         1     0
#>  2         2     0
#>  3         3     1
#>  4         4     2
#>  5         5     3
#>  6         6     3
#>  7         7     4
#>  8         8     4
#>  9         9     5
#> 10        10     5

Created on 2022-07-06 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading