How to extract the maximum value from named vector in R?


To extract the maximum value from named vector in R, we can use which.max function.

For example, if we have a vector called X which is a named vector then we can use the following command to find the maximum value in X.

X[which.max(X)]

Check out the below examples to understand how it works.

Example 1

Following snippet creates a vector −

x1<-rpois(200,10)
names(x1)<-sample(LETTERS[1:4],200,replace=TRUE)
x1

The following vector is created −

 D A  B  D C  B  A C  A  A B  D D  C D B B C A  A  A  A  B D  C D
10 11 8 12 7 12 12 7 10 11 9 16 4 10 8 8 7 7 4 12 12 10 10 6 10 12
 A C  D  C C C C C A  D  B B B A  C C  C A  A  C  A C  C  C D D
11 9 11 13 9 8 7 4 9 18 13 9 8 6 14 8 10 11 7 11 12 12 9 13 8 7
 C  B  A C C  D C C A B  A  A D B  D  C C B C C B  D C  B A  B
18 13 14 8 5 16 7 3 8 8 12 10 5 9 10 13 8 8 6 6 3 12 15 7 6 12
 D B D  A A  A  B A D  B D  D B B B B A  A A  A  C B  D D D  B
14 7 7 13 14 5 10 5 4 11 12 7 7 9 7 7 7 11 11 5 10 14 5 7 8 13
 D  A D  A  A C A  C   A B B B  A A B  C D  A  C C D  D A  B A D
17 11 9 12 11 3 12 11 16 8 9 7 16 8 5 13 13 9 10 7 5 15 5 12 4 8
 C D  A A  D D  A  D D B  B B   D B A  B D B  C C  D D  B  C C  D
13 4 12 7 10 9 12 13 6 6 11 13 13 8 11 8 7 9 10 7 12 11 9 12 6 10
 A  C B   C B D  D C  C  C C  A A  A  B C  A A A D B D  A A B  B
13 10 21 13 6 7 14 9 12 16 8 10 14 9 12 4 10 8 8 9 9 8 11 7 9 12
 B  A  B  A D A  D  C A  C  A D  B  B  B D B  D
11 12 13 16 9 12 4 10 9 14 10 9 12 11 11 7 9 16

To extract the maximum value from named vector in R, add the following code to the above code −

x1<-rpois(200,10)
names(x1)<-sample(LETTERS[1:4],200,replace=TRUE)
x1[which.max(x1)]

Output

If you execute all the above given snippets as a single program, it generates the following output −

B
21

Example 2

Following snippet creates a vector −

x2<-rpois(150,1)
names(x2)<-sample(c("India","China","UK","USA"),150,replace=TRUE)
x2

The following vector is created −

USA China China India USA UK India India USA UK USA China China
 0   1     2     1     0   2   1    1     3   0   2    0    1
China USA UK UK China UK India India China China India UK UK
  1    2   1  1   1    0    1    2     1     1    2    0   0
China UK India China China China UK India UK USA USA China China
   1   0   0    0     1     1     0   0    0  1   0    2     1
UK China USA India India UK India UK India India India India UK
 1   1    0   1     1    0    0    0   1    0      0     0    3
UK UK China USA USA China India USA India UK China China USA
 0  0   2    1   0    0     0    3    2    0    0    2     2
USA China China India China UK UK UK UK UK USA USA China
  2   1    2     1     2     2  2  1  0  2  2   0     2
China India China USA USA USA UK China China UK USA India UK
   0     0    2     1   0   1  1    0     1   1   1   1    2
India India UK USA USA UK UK China India UK India UK India
  2    1     2   1   1   1  1  1    0     1   0    1    3
China China China UK India USA India USA India India USA USA UK
   2    0     0    0   2    0    0     0   0    4     1   0   1
India USA UK China UK India USA USA UK UK USA India India
    1  0   1   1    1   3    0   1   2  0  0   0      0
UK China China USA USA UK UK UK UK India UK USA India
 1  0      1    0    0   1  0 1  2  1    1    1    1
USA USA China China India China India
 2   1    5     1      1     0     0

To extract the maximum value from named vector in R, add the following code to the above code −

x2<-rpois(150,1)
names(x2)<-sample(c("India","China","UK","USA"),150,replace=TRUE)
x2[which.max(x2)]

Output

If you execute all the above given snippets as a single program, it generates the following output −

China
5

Example 3

Following snippet creates a vector −

x3<-round(rnorm(100),4)
names(x3)<-sample(c("ID1","ID2","ID3","ID4"),100,replace=TRUE)
x3

The following vector is created −

   ID2    ID1   ID1    ID4     ID1      ID1     ID1     ID3     ID2    ID3
1.0334 1.7886 0.6770 -0.9339 -0.0034 -0.6387 -0.5268 -0.5980 -0.4164 1.6412
   ID4   ID3     ID1    ID2     ID1     ID4     ID2     ID2    ID4    ID1
0.5849 -0.3585 0.3762 0.7623 -0.2129 -0.9676 -1.7943 -0.0315 0.1025 -0.6559
    ID4   ID3    ID4    ID2     ID2    ID2    ID4    ID3      ID3    ID4
-0.0156 0.0006 0.1971 0.2190 -0.5205 0.3816 0.7501 -0.6530 -0.5844 -1.4492
   ID1    ID4     ID1   ID1     ID2      ID4   ID4      ID1     ID3   ID4
2.3666 -0.2599 0.0630 -0.5157 0.6908 -1.4769 -0.7415 -0.6776 -0.5526 0.0715
    ID1  ID2      ID3     ID1     ID2    ID4    ID4     ID3    ID3    ID3
-1.4454 0.5870 -0.9305 -0.2980 -0.0880 0.6264 0.1578 -0.8833 -2.0587 0.9624
   ID2    ID2     ID3    ID3    ID4    ID4   ID1      ID2    ID4     ID4
-1.5744 -0.8778 0.8962 0.4155 1.9931 0.4885 -0.1609 1.8387 1.1193 -0.9107
   ID2   ID2      ID4    ID1     ID3     ID3    ID3    ID4    ID2     ID4
0.3475 -1.8570 -0.6960 1.0803 -1.0373 -0.1382 0.6719 -1.3278 -0.2718 -0.2137
   ID4     ID1   ID4     ID2    ID2    ID1    ID3     ID4      ID2     ID4
1.8999 -0.8459 0.3388 -0.1979 0.0442 -0.5485 -0.1884 -0.0564 -0.7241 -1.6733
    ID  3 ID2    ID1     ID2    ID3    ID2     ID3     ID1     ID3     ID2
-2.1790 1.7054 0.2405 0.2375 -0.0618 -0.1500 -1.7940 -2.0460 -1.5794 0.6471
   ID4    ID2   ID1     ID1    ID2     ID4    ID2    ID3     ID1      ID1
1.5265 0.3566 1.0487 -0.6227 -1.9454 2.6662 -0.4045 -0.6129 -0.6179 -1.2167

To extract the maximum value from named vector in R, add the following code to the above code −

x3<-round(rnorm(100),4)
names(x3)<-sample(c("ID1","ID2","ID3","ID4"),100,replace=TRUE)
x3[which.max(x3)]

Output

If you execute all the above given snippets as a single program, it generates the following output −

ID4
2.6662

Updated on: 23-Nov-2021

744 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements