- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 remove duplicate rows and sort based on a numerical column an R data frame?
If we have duplicate rows in an R data frame then we can remove them by using unique function with data frame object name. And if we want to order the data frame with duplicate rows based on a numerical column then firstly unique rows should be found then order function can be used for sorting as shown in the below examples.
Example
Consider the below data frame −
x1<-rep(c(2,7,1,5),5) x2<-rep(LETTERS[1:4],5) df1<-data.frame(x1,x2) df1
Output
x1 x2 1 2 A 2 7 B 3 1 C 4 5 D 5 2 A 6 7 B 7 1 C 8 5 D 9 2 A 10 7 B 11 1 C 12 5 D 13 2 A 14 7 B 15 1 C 16 5 D 17 2 A 18 7 B 19 1 C 20 5 D
Finding unique rows of df1 −
Example
df1<-unique(df1) df1
Output
x1 x2 1 2 A 2 7 B 3 1 C 4 5 D
Ordering df1 based on x1 −
Example
df1[order(df1$x1),]
Output
x1 x2 3 1 C 1 2 A 4 5 D 2 7 B
Example
y1<-rep(c(501,278,357,615),5) y2<-rep(c("G1","G2","G3","G4"),5) df2<-data.frame(y1,y2) df2
Output
y1 y2 1 501 G1 2 278 G2 3 357 G3 4 615 G4 5 501 G1 6 278 G2 7 357 G3 8 615 G4 9 501 G1 10 278 G2 11 357 G3 12 615 G4 13 501 G1 14 278 G2 15 357 G3 16 615 G4 17 501 G1 18 278 G2 19 357 G3 20 615 G4
Finding unique rows of df2 −
Example
df2<-unique(df2) df2
Output
y1 y2 1 501 G1 2 278 G2 3 357 G3 4 615 G4
Ordering df2 based on y1 −
Example
df2[order(df2$y1),]
Output
y1 y2 2 278 G2 3 357 G3 1 501 G1 4 615 G4
- Related Articles
- How to subset an R data frame based on numerical and categorical column?
- How to subset rows of an R data frame based on duplicate values in a particular column?
- How to sort a numerical factor column in an R data frame?
- How to remove rows containing missing value based on a particular column in an R data frame?
- How to remove rows based on blanks in a column from a data frame in R?
- How to remove rows from an R data frame based on frequency of values in grouping column?
- How to divide row values of a numerical column based on categorical column values in an R data frame?
- How to remove rows from data frame in R based on grouping value of a particular column?
- Replace numerical column values based on character column values in R data frame.
- How to subset rows based on criterion of multiple numerical columns in R data frame?
- How to select rows based on range of values of a column in an R data frame?
- How to select top rows of an R data frame based on groups of factor column?
- How to remove duplicate rows in an R data frame if exists in two columns?
- How to delete rows of an R data frame based on string match?
- How to remove a column from an R data frame?

Advertisements