# How to select top or bottom n elements of a vector in R?

R ProgrammingServer Side ProgrammingProgramming

Selection of top or bottom elements can be done with the help of head and tail function in R. It is required when we want to understand the data in a vector or perform some calculation for partial data.

## Example

Consider the below vectors, we will use head and tail to select top and bottom elements in these vectors by using positive and negative signs. These will have a different way to select the elements.

> x<-letters[1:26]
> x
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
[1] "a" "b" "c"
[1] "a" "b" "c" "d" "e"
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> tail(x,3)
[1] "x" "y" "z"
> tail(x,5)
[1] "v" "w" "x" "y" "z"
> tail(x,10)
[1] "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
> tail(x,-3)
[1] "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"
[20] "w" "x" "y" "z"
> tail(x,-5)
[1] "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x"
[20] "y" "z"
> tail(x,-10)
[1] "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w"
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u"
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p"
> y<-1:50
> y
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
[1] 1 2 3 4 5
[1] 1 2 3 4 5 6 7 8 9 10
> tail(y,5)
[1] 46 47 48 49 50
> tail(y,10)
[1] 41 42 43 44 45 46 47 48 49 50
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
> tail(y,-5)
[1] 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
[26] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> tail(y,-10)
[1] 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
[26] 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> set.seed(9)
> z<-sample(letters[1:20],30,replace=TRUE)
> z
[1] "f" "s" "c" "l" "p" "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b"
[20] "n" "s" "c" "n" "e" "i" "f" "k" "c" "k" "q"
[1] "f" "s" "c"
[1] "f" "s" "c" "l" "p"
[1] "f" "s" "c" "l" "p" "l" "e" "r" "e" "j"
> tail(z,3)
[1] "c" "k" "q"
> tail(z,5)
[1] "f" "k" "c" "k" "q"
> tail(z,10)
[1] "s" "c" "n" "e" "i" "f" "k" "c" "k" "q"
[1] "f" "s" "c" "l" "p" "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b"
[20] "n" "s" "c" "n" "e" "i" "f" "k"
[1] "f" "s" "c" "l" "p" "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b"
[20] "n" "s" "c" "n" "e" "i"
[1] "f" "s" "c" "l" "p" "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b"
[20] "n"
> tail(z,-3)
[1] "l" "p" "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b" "n" "s" "c"
[20] "n" "e" "i" "f" "k" "c" "k" "q"
> tail(z,-5)
[1] "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b" "n" "s" "c" "n" "e"
[20] "i" "f" "k" "c" "k" "q"
> tail(z,-10)
[1] "l" "c" "p" "i" "a" "f" "b" "o" "b" "n" "s" "c" "n" "e" "i" "f" "k" "c" "k"
[20] "q"
Published on 10-Aug-2020 13:16:32