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

Count number of values less than an increasing value

I have a data frame where I want to count the number of values less or equal to 30, then count the number of values less than or equal to 60, less than or equal to 90…etc. I can do this iteratively using the sum(dat$values <= 30) but I would like to automate this somehow.

Example

set.seed(917)
dat <- as.data.frame(sample(0:2100,100,replace = F))
colnames(dat)[1] <- "values"

sum(dat$values <= 30)
sum(dat$values <= 60)
sum(dat$values <= 90)
sum(dat$values <= 120)
sum(dat$values <= 150)

Output should look like

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

  BinWidth TotalValuesLessThan
1       30                   1
2       60                   1
3       90                   2
4      120                   3
5      150                   4

I would like to have the output iteratively go out to BinWidth = 2100 without needing to use sum(dat$values <= X) 70 times.

>Solution :

Just create your bins and apply along them.

BinWidth <- seq(30, 2100, by = 30)
TotalValueLessThan <- sapply(BinWidth, \(x) sum(dat$values <= x))

head(data.frame(BinWidth, TotalValueLessThan))
#>   BinWidth TotalValueLessThan
#> 1       30                  0
#> 2       60                  1
#> 3       90                  3
#> 4      120                  6
#> 5      150                  6
#> 6      180                 10

Change \(x) to function(x) if you are using R < 4.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