Here, we show some basic data operations in R such as sort, reverse, unique values, duplicate values, rank, head values, and tail values.
All functions are from the "base" package, except
head()
and tail()
which are from the "utils"
package.
Function | Usage |
sort() |
Sort data |
rev() |
Reverse vector elements |
unique() |
Return unique elements |
duplicated() |
Find duplicated elements |
rank() |
Rank in numeric or alphanumeric order |
head() |
See first few values of data |
tail() |
See last few values of data |
Sort letters or words in a vector:
[1] "A" "B" "C" "D" "E"
Sort numbers:
[1] 2 2 3 4 5 7 7 8
Sort numbers in decreasing order:
[1] 8 7 7 5 4 3 2 2
Reverse order of vector elements:
[1] 35 48 46 43 32 39
Get unique elements:
The unique elements will be listed.
[1] 4 2 7 8 5 3
Find duplicated values:
TRUE
will be returned in the positions of duplicated
elements.
[1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE
Return numeric rank order of elements:
Numeric ranks will be returned with "1" for the lowest number.
[1] 2 1 4 3 5
Return alphanumeric rank order of elements:
Alphanumeric ranks will be returned with "1" for the first number on the alphanumeric order.
[1] 4 3 1 2
[1] 1 3 4 7 2 6 5
Using the dataframe below:
Letter Number
1 A 1
2 B 2
3 C 3
4 D 4
5 E 5
6 F 6
Return only the first 3 rows:
Letter Number
1 A 1
2 B 2
3 C 3
Return only the last 2 rows:
Letter Number
5 E 5
6 F 6
Return only the first 3 or last 3 values of a vector:
[1] 4 6 2
[1] 8 9 5
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes