- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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"
- Related Articles
- How to select random elements from an R vector?
- How to replace one vector elements with another vector elements in R?
- How to remove some last elements of a vector in R?
- How to create unordered triplets of a vector elements in R?
- How to select multiple elements of a list in R?
- How to divide columns of a matrix by vector elements in R?
- MySQL query to select bottom n records
- How to convert the repeated elements of strings in a vector to unique elements in R?
- How to manage top and bottom spaces of a bar plot using ggplot2 in R?
- How to find the intersection of elements in a string vector in R?
- How to print the vector elements vertically in R?
- How to sort a vector in R based on manual position of elements?
- How to find all combinations of a vector elements without space in R?
- How to select columns of an R data frame that are not in a vector?
- How to convert unit of measurements of a value or a vector in R?

Advertisements