How to find the frequency of values greater than or equal to a certain value in R?

R ProgrammingServer Side ProgrammingProgramming

In Data Analysis, we often need to look for less than, less than equal to, greater than, or greater than equal to values to compare them with some threshold. Sometimes we also require the frequency of these values. Therefore, we can use sum function for this purpose. For example, if a vector x has 10 integer values then to check how many of them are greater than or equal to 10, we can use the command sum(x>=10).

Example1

 Live Demo

x1<−rpois(150,5)
x1

Output

[1] 4 5 3 3 5 4 9 4 7 1 6 15 3 3 2 3 5 5 3 6 9 6 5 2 1
[26] 4 2 1 6 3 8 2 6 7 6 5 5 7 6 3 3 4 5 3 8 11 0 3 7 5
[51] 5 4 7 3 4 6 4 4 9 8 3 6 10 4 3 7 3 6 5 4 7 5 3 1 3
[76] 6 4 5 2 11 5 6 5 2 4 3 6 8 3 5 8 8 2 7 10 5 4 4 3 3
[101] 8 7 5 7 3 5 5 3 9 3 3 8 5 5 5 5 7 5 7 5 4 7 4 8 5
[126] 4 4 7 7 4 2 1 7 7 4 6 7 6 6 3 2 3 7 4 3 6 8 4 3 8

Example

sum(x1>=5)

Output

[1] 83

Example2

 Live Demo

x2<−rpois(150,2)
x2

Output

[1] 0 6 3 2 3 2 1 1 0 3 0 5 2 0 3 1 3 1 2 3 2 3 2 5 0 3 0 3 0 3 3 1 2 3 3 1 3
[38] 2 2 2 2 2 2 1 3 2 0 2 1 4 2 1 1 2 4 0 2 2 1 0 1 4 2 1 2 4 1 2 0 3 2 2 1 0
[75] 0 3 1 2 1 1 4 4 1 2 2 0 1 3 3 5 5 1 5 2 1 2 2 3 3 3 2 3 2 1 2 1 5 0 3 2 1
[112] 3 2 1 3 4 2 4 3 4 1 3 4 1 1 1 2 3 1 3 2 0 2 2 2 2 3 2 3 1 1 4 1 5 3 3 1 1
[149] 0 0

Example

sum(x2>=5)

Output

[1] 8

Example3

 Live Demo

x3<−round(rnorm(100),2)
x3

Output

[1] −1.45 0.32 −0.04 0.96 0.73 −0.23 −0.74 0.53 −1.78 2.12 1.52 1.51
[13] 1.98 −0.57 −0.88 −2.06 0.44 −0.33 1.03 1.63 1.55 0.34 −0.17 −0.79
[25] 0.68 −0.64 −0.82 1.04 −1.02 0.05 1.07 −1.50 −2.70 0.99 0.07 −0.97
[37] −0.08 −1.32 0.51 0.01 0.92 −1.11 −0.32 1.05 0.19 0.49 −0.06 1.38
[49] 0.04 0.06 1.57 −0.09 1.35 0.83 0.54 1.70 −1.36 0.31 −1.53 −0.07
[61] 0.07 0.61 0.13 −0.82 −0.24 −1.40 0.71 −0.77 −0.08 −0.06 0.10 −1.07
[73] −0.75 0.29 −0.11 −0.58 −1.01 0.06 −1.06 0.32 −0.21 −0.61 0.98 0.12
[85] −0.58 −0.56 1.11 −0.29 0.21 0.53 −0.29 1.13 0.03 −0.04 1.85 −1.22
[97] −0.23 −1.45 −1.68 0.27

Example

sum(x3>=0.25)

Output

[1] 38

Example4

 Live Demo

x4<−rnorm(100,10,3)
x4

Output

[1] 7.597491 7.949568 8.126105 7.463872 6.617113 8.354158 8.803647
[8] 9.424731 14.517557 9.776073 14.094525 14.051761 6.694324 10.161498
[15] 6.684266 8.487643 13.053916 8.888167 8.633980 12.356086 11.405917
[22] 10.707835 8.051093 2.950978 12.248564 7.022646 10.238748 13.662946
[29] 10.866488 7.355724 16.510802 13.140203 7.632991 8.343411 7.714010
[36] 10.462814 6.513682 5.024443 9.283309 9.174802 10.994014 11.732416
[43] 10.227234 7.312629 8.476452 9.001631 11.709300 9.743112 12.810862
[50] 12.298896 9.539788 13.943619 11.345357 9.963804 13.229894 5.094773
[57] 10.099810 7.648431 11.704927 8.428569 4.880670 14.992570 10.566891
[64] 12.549172 13.232136 4.453650 8.361071 11.638091 12.893968 8.072887
[71] 14.663789 7.899722 10.907741 4.416071 12.089033 14.545859 19.397030
[78] 6.023658 8.473864 8.215500 2.549976 6.273917 17.355490 7.472054
[85] 11.106132 9.293263 10.283507 12.538926 17.778788 12.705169 7.679622
[92] 10.750032 6.393891 12.565089 5.556969 12.610292 17.343324 15.736677
[99] 9.505158 11.343209

Output

sum(x4>=10)

Output

[1] 49

Example5

 Live Demo

x5<−runif(100,2,5)
x5

Output

[1] 3.636398 3.898034 4.296205 2.249604 2.379428 3.233782 4.798916 3.150175
[9] 3.526049 3.318808 2.092628 3.640334 4.776549 2.145952 4.751566 3.429172
[17] 2.197853 2.783967 2.018186 2.002520 2.579248 3.083350 3.924225 3.271760
[25] 2.556557 2.416284 3.140659 4.329581 2.354663 2.951233 2.386200 3.322287
[33] 3.178507 2.592847 4.553768 4.011444 2.467380 2.325412 3.087714 2.945327
[41] 4.635870 3.877260 2.586140 3.005279 4.753057 3.766665 2.614960 3.958394
[49] 2.167482 3.436171 4.089592 2.031541 3.491617 4.199338 2.635759 3.255678
[57] 3.798499 3.738574 2.895809 3.674032 4.137452 4.003497 2.231915 4.378317
[65] 2.938005 3.695278 3.475259 3.673781 3.564739 4.816054 3.876752 2.127490
[73] 2.341472 4.316100 3.912831 4.377490 3.600701 3.091522 3.442127 2.196780
[81] 3.584403 3.917059 3.135924 3.425142 3.590885 3.860618 2.778151 2.195585
[89] 3.199603 2.247892 4.830438 4.290870 2.662993 2.749634 4.931301 2.567786
[97] 3.551937 4.484705 2.348458 2.189373

Example

sum(x5>=4)

Output

[1] 21
raja
Published on 09-Feb-2021 11:51:59
Advertisements