How to calculate the number of elements greater than a certain value in a vector in R?

R ProgrammingServer Side ProgrammingProgramming

In data analysis, sometimes we need to count the number of values that are greater than or less than a certain value, and this certain value could be a threshold. For example, we might have a vector that contain values for blood pressure of people and we might want check how many values are greater than 120. In this type of situation, we can use length function as shown in the below examples.

Example

 Live Demo

x1<-1:10
x1

Output

[1] 1 2 3 4 5 6 7 8 9 10

Example

length(x1[x1>1])

Output

[1] 9

Example

 Live Demo

x2<-rpois(100,10)
x2

Output

[1] 7 8 8 11 11 10 5 8 10 7 13 12 14 13 13 11 6 17 17 13 18 8 13 11 11
[26] 10 10 7 9 10 10 6 11 12 12 10 5 12 14 8 7 5 8 15 9 6 10 10 5 8
[51] 10 10 7 7 11 16 7 9 14 9 16 8 14 3 10 13 11 7 9 6 4 9 14 14 12
[76] 15 10 9 9 11 9 9 14 10 14 9 11 6 9 14 8 7 9 11 9 10 7 12 5 8

Example

length(x2[x2>5])

Output

[1] 93

Example

 Live Demo

x3<-rpois(120,5)
x3

Output

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

Example

length(x3[x3>5])

Output

[1] 42

Example

 Live Demo

x4<-sample(0:9,120,replace=TRUE)
x4

Output

[1] 2 4 5 3 4 3 6 3 2 3 7 5 7 8 0 2 6 0 3 3 1 8 2 1 5 5 3 4 6 3 5 2 6 1 3 9 3
[38] 2 8 6 8 4 9 0 9 2 8 0 5 8 1 5 5 1 2 7 6 8 9 8 8 2 0 0 2 2 2 4 7 2 9 2 5 7
[75] 5 4 2 8 7 3 4 5 2 8 0 2 3 4 6 3 3 3 5 8 4 9 4 8 6 2 9 0 2 6 3 1 2 8 8 4 5
[112] 8 5 7 2 4 0 4 4 0

Example

length(x4[x4>0])

Output

[1] 108

Example

 Live Demo

x5<-sample(1:10,120,replace=TRUE)
x5

Output

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

Example

length(x5[x5>1])

Output

[1] 107

Example

 Live Demo

x6<-round(rnorm(120,5,0.8),0)
x6

Output

[1] 5 5 5 5 5 5 5 5 5 4 3 5 5 5 5 5 3 5 6 3 5 5 5 6 6 6 5 5 5 5 4 5 5 6 6 5 6
[38] 5 4 5 5 5 7 6 6 5 5 6 5 5 4 4 4 6 5 4 6 6 4 5 5 7 7 5 6 5 5 6 4 5 4 6 4 6
[75] 6 5 4 4 5 6 6 6 5 5 4 5 4 4 3 6 6 7 5 6 5 5 5 5 6 7 4 5 4 6 5 7 4 6 5 5 4
[112] 6 5 5 6 5 6 5 5 6

Example

length(x6[x6>5])

Output

[1] 31

Example

 Live Demo

x7<-round(rexp(120,1.2),0)
x7

Output

[1] 0 1 0 1 2 0 0 2 1 0 1 0 0 0 2 0 1 2 2 0 1 0 2 1 3 1 0 0 0 0 1 0 0 2 1 4 1
[38] 0 1 2 0 1 0 1 3 1 0 3 0 0 2 1 1 2 1 1 0 1 1 2 1 0 1 1 0 1 2 0 1 0 0 1 1 1
[75] 2 0 1 0 0 2 0 3 0 2 1 1 0 3 1 1 0 0 1 3 1 1 1 2 0 0 0 0 1 2 0 0 1 0 0 3 1
[112] 2 0 0 1 1 0 0 1 0

Example

length(x7[x7>1])

Output

[1] 21

Example

 Live Demo

x8<-round(runif(120,2,5),0)
x8

Output

[1] 3 3 4 3 5 2 2 4 4 2 3 4 3 3 4 4 5 4 5 5 2 4 3 4 3 3 3 5 2 3 3 2 3 3 5 3 4
[38] 5 4 2 3 3 2 4 2 4 3 4 4 2 3 3 3 4 4 5 4 5 4 3 3 3 5 4 5 3 3 2 5 2 4 3 3 4
[75] 4 4 2 4 4 2 4 2 5 4 3 3 3 3 4 2 4 5 4 3 4 2 2 2 4 3 5 4 3 5 3 4 5 5 3 2 3
[112] 3 4 4 3 2 4 4 2 2

Example

length(x8[x8>4])

Output

[1] 19

Example

 Live Demo

x9<-sample(100:120,120,replace=TRUE)
x9

Output

[1] 103 107 100 103 119 113 104 114 107 116 102 116 115 103 104 103 106 110
[19] 101 106 120 108 111 100 101 107 101 119 113 117 107 119 114 118 115 118
[37] 107 110 116 118 102 118 106 113 112 112 104 119 112 120 115 113 104 120
[55] 105 113 112 119 117 118 103 116 120 107 115 108 105 104 106 106 106 107
[73] 107 112 114 113 111 101 100 107 115 116 111 106 111 108 102 109 108 102
[91] 109 109 112 118 119 109 116 112 101 103 114 119 106 117 120 113 101 118
[109] 109 103 106 109 119 113 111 108 106 106 111 103

Example

length(x9[x9>118])

Output

[1] 11

Example

 Live Demo

x10<-sample(1001:9999,120)
x10

Output

[1] 3488 7457 9491 7422 2716 8570 3510 7737 1234 9748 1222 7851 9055 7677 2331
[16] 1511 3830 7976 3207 6283 8621 8166 1846 8450 6794 2995 5714 4353 6299 5056
[31] 3793 2882 4625 6558 4089 5635 5463 4580 7773 6027 2602 1970 9832 1240 9925
[46] 2786 9829 3931 9500 9508 2617 1652 7741 1560 3448 9895 1329 8854 5187 2161
[61] 2404 4165 7624 1123 5403 8007 5057 5184 3152 8221 2635 5477 9033 6386 6171
[76] 3779 5946 2944 4102 5216 9475 6895 4553 6728 3989 7808 1869 8465 9322 5939
[91] 9161 9620 7835 3986 8397 5404 7229 5472 3104 3584 5423 3961 5396 6813 1986
[106] 7241 7724 6955 6082 8359 1904 3839 7441 6690 9601 8432 4561 7033 2314 3743

Example

length(x10[x10>5000])

Output

[1] 68
raja
Published on 19-Oct-2020 17:18:34
Advertisements