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
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 1:
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.