I have a long list of random numbers between 1 and 100, and i would like to count how many of them are larger than 10,20,30 etc
x <- c(sample(1:100, 500, replace = T))
y <- seq(0,100, by = 10)
I am looking for this to return an output such as;
Total | 10 | 20 | 30 | 40 | 50 |
---|---|---|---|---|---|
Count | 7 | 13 | 17 | 28 | 42 |
Where Count is the number of x Values that are larger than Total (each y value )
So far, I have tried
Count = ifelse(x > y, 1, 0)
However this returns a list of Binary 1,0 returns for each of the 500 values of X
I'd appreciate any help
CodePudding user response:
This answer asummes your looking for intervals not for cummulative sum of numbers greater than a threshold given your count.
cut
table
are useful here:
table(cut(x, breaks = y))
(0,10] (10,20] (20,30] (30,40] (40,50] (50,60] (60,70] (70,80] (80,90] (90,100]
51 66 36 44 54 49 55 46 58 41
findInterval
table
will give you the same result
table(findInterval(x, y, left.open = TRUE))
Data
set.seed(505)
x <- c(sample(1:100, 500, replace = T))
y <- seq(0,100, by = 10)
CodePudding user response:
If I understood correctly, this might work:
x <- c(sample(1:100, 500, replace = T))
y <- seq(0,100, by = 10)
is_bigger_than <- function(y){
data.frame(y, n = sum(x > y,na.rm = TRUE))
}
purrr::map_df(y,is_bigger_than)
y n
1 0 500
2 10 450
3 20 403
4 30 359
5 40 305
6 50 264
7 60 201
8 70 155
9 80 100
10 90 52
11 100 0
CodePudding user response:
With base R this is one approach
rbind(Total = y, Count = rowSums(sapply(x, ">", y)))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
Total 0 10 20 30 40 50 60 70 80 90 100
Count 500 444 381 329 279 241 198 150 104 52 0