 # R Program to find second highest value in a given vector

## How to access the second-highest value in a given vector

Here we are explaining how to write an R program to access the second-highest value in a given vector. Here we are using a built-in function sort() for this finding. The sort() function helps to sort a vector by its values. The sorting can be possible in both ascending and descending order. By default, it sorts in ascending order for making sorting in ascending order need to set decreasing=TRUE. The syntax of sorting is like

``````sort(x, decreasing, na.last) #Where x is a vector
```
```

Here x is the vector to be sorted, decreasing is the Boolean value to sort in descending order, and na.last is the Boolean value to put NA at the end.

## How to find the second-highest value of a vector in the R program

Below are the steps used in the R program to get the last value in a given vector. In this R program, we directly give the values to built-in functions. For that first, we find the length of the vector into variable l and the vector value is assigned into the variable A. Finally, call the sort function as sort(x, partial = l-1)[l-1] for finding second largest element.

## ALGORITHM

STEP 1: Assign variable A with vector values

STEP 2: First print original vector values

STEP 3: assign the length of a vector by using length function as length(A)

STEP 4: Call the function sort as sort(A, partial = l-1)[l-1]

STEP 5: print the result of the function

## R Source Code

```                                          ```A = c(20, 32, 30, 10, 40, 25, 30, 27)
print("Original Vectors:")
print(A)
print("Find second highest value in a given vector:")
l = length(A)
print(sort(A, partial = l-1)[l-1])
```
```

## OUTPUT

``` "Original Vectors:"
 20, 32, 30, 10, 40, 25, 30, 27
 "Find second highest value in a given vector:"
 32
```
VIEW ALL
VIEW ALL
##### OtherTutorials
VIEW ALL 