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


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"
> head(x,3)
[1] "a" "b" "c"
> head(x,5)
[1] "a" "b" "c" "d" "e"
> head(x,10)
[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"
> head(x,-3)
[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"
> head(x,-5)
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u"
> head(x,-10)
[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
> head(y,5)
[1] 1 2 3 4 5
> head(y,10)
[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
> head(y,-5)
[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
> head(y,-10)
[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"
> head(z,3)
[1] "f" "s" "c"
> head(z,5)
[1] "f" "s" "c" "l" "p"
> head(z,10)
[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"
> head(z,-3)
[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"
> head(z,-5)
[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"
> head(z,-10)
[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"

Updated on: 10-Aug-2020

411 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements