How to count the number of values that satisfy a condition in an R vector?

R ProgrammingServer Side ProgrammingProgramming

Sometimes we want to find the frequency of values that satisfy a certain condition. For example, if we have a vector say x that contains randomly selected integers starting from 1 and ends at 100, in this case we might want to find how many values are exactly equal to 10. This can be done by using which and length function.

Example1

Live Demo

> x1<-rpois(10,5)
> x1

Output

[1] 5 7 3 3 2 7 3 7 6 3

Example

> length(which(x1==5))
[1] 1
> length(which(x1==7))
[1] 3
> length(which(x1==3))
[1] 4

Example2

Live Demo

> x2<-sample(0:9,300,replace=TRUE)
> x2

Output

[1] 4 1 5 5 5 3 8 9 8 4 8 1 6 3 6 1 0 0 9 4 5 4 5 1 2 3 3 3 4 9 1 4 7 0 7 7 8
[38] 3 5 8 0 3 6 5 0 7 9 8 2 7 3 9 6 5 2 7 6 4 6 3 3 7 7 2 2 6 0 0 1 5 1 2 9 8
[75] 9 9 3 9 0 9 5 7 9 1 0 4 7 2 7 9 9 5 8 9 2 8 1 3 3 8 4 8 5 5 2 4 5 8 0 8 4
[112] 4 5 2 4 5 9 8 5 5 9 4 4 6 6 9 2 5 2 9 0 3 8 7 5 8 1 3 1 0 2 6 8 5 6 1 0 2
[149] 6 3 6 2 4 2 9 2 7 8 3 8 4 7 3 1 9 3 2 0 7 0 3 8 9 7 1 2 3 5 8 9 0 4 6 9 8
[186] 6 7 5 6 3 5 3 4 3 7 6 8 0 1 1 7 8 7 4 1 2 3 9 7 5 0 0 6 3 5 8 1 2 1 6 5 2
[223] 1 0 5 0 8 3 9 2 6 5 2 6 3 7 7 1 4 3 7 6 1 2 3 4 7 2 5 5 5 8 4 2 0 9 1 7 1
[260] 2 1 9 8 2 9 1 5 2 0 5 0 0 1 2 1 7 6 3 8 4 1 9 8 0 9 2 1 6 7 5 1 2 7 2 9 5
[297] 6 8 7 1

Example

> length(which(x2==0))
[1] 26
> length(which(x2==1))
[1] 32
> length(which(x2==2))
[1] 34
> length(which(x2==3))
[1] 31
> length(which(x2==4))
[1] 24
> length(which(x2==5))
[1] 36
> length(which(x2==6))
[1] 25
> length(which(x2==7))
[1] 30
> length(which(x2==8))
[1] 31
> length(which(x2==9))
[1] 31

Example3

Live Demo

> x3<-sample(501:999,200)
> x3

Output

[1] 977 775 630 693 624 602 903 755 819 779 754 914 731 507 946 836 780 826
[19] 718 899 762 897 583 880 712 669 551 620 959 753 832 835 786 725 803 881
[37] 520 541 795 691 957 888 635 738 562 673 746 665 807 855 666 778 664 657
[55] 985 752 701 967 513 548 997 504 814 722 646 893 992 975 675 645 870 760
[73] 920 894 909 770 982 613 817 581 651 704 568 911 763 523 766 872 929 816
[91] 535 518 956 689 802 848 822 621 506 632 991 811 842 570 923 838 873 567
[109] 812 720 884 958 653 879 854 859 663 919 680 833 601 994 988 576 839 950
[127] 698 768 649 517 626 867 690 594 952 623 895 783 892 970 785 815 672 813
[145] 863 682 986 707 948 983 544 622 606 761 937 906 990 740 688 844 799 684
[163] 589 960 998 976 515 627 925 658 685 913 694 596 784 933 687 522 503 600
[181] 751 558 856 525 963 827 514 907 774 579 509 508 700 717 900 656 918 901
[199] 532 829

Example

> length(which(x3==501))
[1] 0
> length(which(x3==502))
[1] 0
> length(which(x3==620))
[1] 1
> length(which(x3==920))
[1] 1

Example4

Live Demo

> x4<-round(runif(300,5,10),0)
> x4

Output

[1] 8 5 9 5 9 10 8 5 6 7 8 10 7 5 8 10 10 9 7 6 9 6 10 8 9
[26] 10 7 6 7 10 9 9 8 5 9 6 5 7 7 8 5 7 7 7 6 7 9 9 9 9
[51] 6 8 9 8 8 6 5 8 9 9 5 9 9 6 8 9 6 6 7 10 9 7 9 8 9
[76] 10 8 9 6 10 7 8 10 6 6 6 7 5 7 5 10 10 7 5 10 6 7 7 6 5
[101] 5 7 7 10 10 10 9 10 9 8 10 9 9 7 10 8 10 9 6 9 8 9 8 7 6
[126] 9 10 7 8 9 5 6 6 10 5 5 8 9 7 8 5 6 9 8 10 6 10 6 7 5
[151] 6 6 8 5 8 9 10 7 6 8 7 8 5 7 7 8 8 9 7 6 5 9 8 6 9
[176] 6 7 8 5 5 5 10 8 5 9 7 5 7 9 9 6 9 8 9 6 10 7 5 9 6
[201] 9 7 6 5 9 6 5 8 7 8 6 7 7 8 8 9 9 7 9 6 8 7 9 8 8
[226] 9 6 7 9 9 5 7 6 8 9 10 9 9 8 9 7 5 7 5 8 5 9 5 6 7
[251] 6 7 7 5 7 10 7 8 8 7 7 9 10 6 6 9 9 7 8 10 6 8 6 7 7
[276] 10 9 8 10 7 9 9 9 8 7 6 9 7 9 8 5 6 10 6 10 8 9 7 8 10

Example

> length(which(x4==5))
[1] 37
> length(which(x4==6))
[1] 48
> length(which(x4==7))
[1] 59
> length(which(x4==8))
[1] 52
> length(which(x4==9))
[1] 67
> length(which(x4==10))
[1] 37

Example5

Live Demo

> x5<-sample(11:15,150,replace=TRUE)
> x5

Output

[1] 12 15 12 11 15 15 15 15 13 12 12 11 15 15 13 15 15 14 14 12 15 11 13 14 11
[26] 13 13 15 13 13 15 12 13 15 12 12 12 13 14 13 12 14 15 11 14 13 14 14 12 13
[51] 11 11 13 11 12 12 15 15 14 11 11 11 14 13 14 13 12 12 11 12 12 14 13 13 15
[76] 11 11 14 14 14 14 12 11 12 13 15 12 12 15 14 14 13 12 13 15 11 11 14 13 12
[101] 15 14 15 12 13 13 15 11 14 11 14 15 12 11 13 13 12 11 15 12 13 14 11 15 12
[126] 12 11 14 15 12 12 14 14 11 14 11 12 13 12 15 13 11 15 11 13 12 12 14 13 11

Example

> length(which(x5==11))
[1] 28
> length(which(x5==12))
[1] 35
> length(which(x5==13))
[1] 30
> length(which(x5==14))
[1] 28
> length(which(x5==15))
[1] 29
raja
Published on 07-Nov-2020 07:37:11
Advertisements