How to find the index of the last occurrence of repeated values in a vector in R?


Indexing helps us to understand the location of the value in the vector. If we have a vector that contains repeated values then we might want to figure out the last occurrence of the repeated value. For example, if we have a vector x that contains 1, 1, 2, 1, 2 then the last occurrence of repeated values will be 4 and 5 because the last 1 is at 4th position and 2 is at the 5th position. We can find this by using tapply function in R.

Example

 Live Demo

x1<-sample(0:3,100,replace=TRUE)
x1

Output

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

tapply(seq_along(x1),x1,max)

0 1 2 3
94 98 95 100

Example

 Live Demo

x2<-sample(0:10,120,replace=TRUE)
x2

Output

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

tapply(seq_along(x2),x2,max)

0 1 2 3 4 5 6 7 8 9 10
105 120 109 84 115 119 101 118 117 108 116

Example

 Live Demo

x3<-sample(0:50,120,replace=TRUE)
x3

Output

[1] 43 19 25 49 38 44 3 42 6 1 6 43 46 37 21 43 19 31 37 31 14 7 7 1 31
[26] 20 21 42 21 7 10 46 24 13 12 47 36 8 8 42 14 35 7 19 39 24 10 7 30 27
[51] 44 33 50 34 3 36 32 39 22 3 38 22 23 33 18 17 11 32 35 37 42 35 37 48 15
[76] 33 44 41 40 7 10 43 2 4 37 3 24 19 15 44 20 25 22 16 48 19 15 36 2 15
[101] 26 3 42 40 31 17 13 21 46 44 33 12 17 12 2 3 18 11 34 43

tapply(seq_along(x3),x3,max)

0 1 2 3 4 5 6 7 9 11 12 13 14 15 16 17 19 20 21 22
103 75 104 55 60 41 118 115 101
111 120 84 79 71 61 47 106 113 94 110 23 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
96 31 69 109 92 90 99 91 102 22 40 13 87 12 70 74 112 93 117 114
44 46 47 48 49
81 38 119 80 76

Example

 Live Demo

x4<-sample(rnorm(10,2),50,replace=TRUE)
x4

Output

[1] 1.579654 1.724472 1.587958 3.020974 1.951833 1.951833 1.724472 1.724472
[9] 3.232319 1.724472 1.579654 1.951833 1.724472 3.140572 3.140572 2.491374
[17] 1.951833 1.579654 3.140572 1.951833 1.579654 3.020974 3.232319 2.491374
[25] 1.579654 3.020974 1.724472 1.724472 3.232319 3.140572 1.951833 1.587958
[33] 3.232319 1.724472 1.587958 1.724472 3.020974 2.491374 1.951833 1.579654
[41] 2.491374 2.276152 1.579654 1.579654 1.951833 2.276152 1.724472 1.724472
[49] 1.312536 1.724472

tapply(seq_along(x4),x4,max)

1.78964110968161 1.79728139652025 2.09899628027542 2.31654203511509
      46               40              50              36
2.42762524515435 2.49110739590719 2.81806655273895 2.81941177761899
       47             48              49               43
2.84785684772923 4.53333410090968
       42              45

Example

 Live Demo

x5<-sample(runif(10,2,5),50,replace=TRUE) x5

Output

[1] 2.510584 4.532413 4.086315 4.532413 4.086315 2.510584 3.642460 2.336327
[9] 3.716678 4.086315 4.532413 2.124497 4.086315 3.716678 4.086315 4.532413
[17] 3.642460 2.124497 3.298562 3.642460 2.098908 2.124497 2.510584 4.532413
[25] 4.532413 4.086315 4.532413 2.336327 3.298562 3.716678 4.532413 3.642460
[33] 3.298562 4.478458 3.642460 4.532413 3.298562 3.642460 3.716678 4.086315
[41] 3.716678 2.510584 2.098908 4.478458 3.642460 3.298562 2.098908 3.298562
[49] 4.478458 4.086315

tapply(seq_along(x5),x5,max)

2.05759489955381 2.29070001118816 2.56976890657097 3.92531121708453
      49               34             50                48
3.96077384171076 4.2632744347211 4.58612308674492 4.76869373139925
      37              39             42                45
4.89123421232216
   47

Example

 Live Demo

x6<-sample(rpois(10,5),120,replace=TRUE)
x6

Output

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

tapply(seq_along(x6),x6,max)

2 3 4 5 7 8
110 120 119 112 113 116

Example

 Live Demo

x7<-sample(rexp(20,5),50,replace=TRUE)
x7

Output

[1] 0.180254286 0.721304706 0.599169976 0.173330448 0.610224542 0.101993073
[7] 0.002024576 0.956174020 0.067450237 0.101993073 0.136168037 0.180254286
[13] 0.180254286 0.610224542 0.213114384 0.136168037 0.721304706 0.002024576
[19] 0.173330448 0.599169976 0.077957945 0.180254286 0.599169976 0.180254286
[25] 0.063333364 0.063333364 0.083537116 0.063333364 0.048428059 0.956174020
[31] 0.101993073 0.956174020 0.599169976 0.002024576 0.073227477 0.610224542
[37] 0.101993073 0.180254286 0.077957945 0.180254286 0.276644929 0.077957945
[43] 0.005943913 0.101993073 0.063333364 0.005943913 0.067450237 0.048428059
[49] 0.048428059 0.173330448

tapply(seq_along(x7),x7,max)

0.0114322041161358 0.0248601489034523 0.0391972061246634 0.0440873181624195
          12             31                 16                  22
0.0520261899568141 0.0552537097595632 0.0913002151064575 0.122266020718962
            40             45                  19              42
0.168316483028207 0.171933523557129 0.26439337375665 0.270982742673698
       4                37                 46                  43
0.28890159910727 0.303847270498291 0.417865602333797 0.46208735888166
        48             50                     25           34
0.495591953744121 0.634115280177365
   47                     27

Example

 Live Demo

x8<-sample(101:999,100)
x8

Output

[1] 549 771 883 740 207 878 248 661 634 485 867 355 718 292 847 963 947 814
[19] 818 522 900 851 776 694 978 747 648 303 172 964 995 154 286 773 377 169
[37] 189 840 561 687 393 430 994 116 599 405 610 323 165 195 115 635 556 390
[55] 149 494 642 294 658 976 185 596 805 496 794 255 461 417 458 787 521 882
[73] 217 972 829 824 376 252 396 247 350 719 618 397 328 845 691 538 307 277
[91] 258 432 233 745 352 542 595 219 529 660

tapply(seq_along(x8),x8,max)

107 108 119 121 123 124 127 131 157 179 195 199 207 208 220 228 233 237 265 270
 22 19 21 25 65 5 99 28 96 3 27 13 44 48 52 91 86 15 32 30
272 275 306 317 331 336 353 355 356 359 370 374 391 407 415 416 418 424 426 460
 36 81 85 46 54 98 74 42 23 37 31 6 39 41 10 59 34 1 64 35
482 484 496 498 509 516 539 544 565 573 577 587 598 613 620 621 642 650 684 685
 88 100 4 82 62 14 2 73 40 76 93 12 95 60 55 87 94 33 53 63
691 696 707 708 715 717 722 723 729 730 741 749 750 763 765 791 792 793 832 840
72 75 67 92 71 26 84 24 51 50 77 61 9 89 56 57 47 16 45 68
 852 863 874 879 881 883 884 886 895 910 912 921 922 934 951 952 956 961 964 983
 70 79 90 69 97 43 66 7 20 29 8 58 18 17 11 78 80 49 83 38

Updated on: 10-Oct-2020

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements