- 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 sort an R data frame column without losing row names?
When we sort a data frame column in R, the row names are lost but we might need them. Therefore, sorting without losing row names is required and it can be done with the help of order function. For example, if we have a data frame called df that has a column x then sorting of x without losing row names can be done by using the below command −
df[order(df$x),,drop=FALSE]
Consider the below data frame −
Example
x1<-rnorm(20) x2<-rnorm(20,525,23.2) df1<-data.frame(x1,x2) row.names(df1)<-LETTERS[1:20] df1
Output
x1 x2 A 1.7337922 534.3863 B -0.6754809 534.8879 C 0.1106191 520.2269 D 0.2270701 513.5676 E -0.5678853 558.2216 F -0.1885840 534.2416 G 0.2192299 552.8982 H 1.5135296 488.6875 I 0.9043322 529.6242 J 0.7014559 552.5001 K 0.2591020 514.1494 L -1.1273898 580.5344 M -0.1771963 516.4953 N 0.2317244 503.3607 O 0.6529487 501.4557 P -0.9165830 496.4222 Q -2.2928809 490.9002 R 0.7511574 519.3586 S 1.5003125 504.2702 T 1.3791592 496.9542
Sorting df1 based on x2 without losing row names −
df1[order(df1$x2),,drop=FALSE]
x1 x2 H 1.5135296 488.6875 Q -2.2928809 490.9002 P -0.9165830 496.4222 T 1.3791592 496.9542 O 0.6529487 501.4557 N 0.2317244 503.3607 S 1.5003125 504.2702 D 0.2270701 513.5676 K 0.2591020 514.1494 M -0.1771963 516.4953 R 0.7511574 519.3586 C 0.1106191 520.2269 I 0.9043322 529.6242 F -0.1885840 534.2416 A 1.7337922 534.3863 B -0.6754809 534.8879 J 0.7014559 552.5001 G 0.2192299 552.8982 E -0.5678853 558.2216 L -1.1273898 580.5344
Example
y1<-rpois(20,5) y2<-rpois(20,8) df2<-data.frame(y1,y2) row.names(df2)<-letters[1:20] df2
Output
y1 y2 a 5 7 b 5 5 c 4 6 d 7 1 e 3 8 f 7 9 g 5 9 h 2 7 i 3 14 j 12 6 k 6 9 l 12 7 m 2 11 n 5 6 o 5 11 p 3 3 q 6 10 r 3 9 s 7 8 t 6 8
Sorting df2 based on y1 without losing row names −
df2[order(df2$y1),,drop=FALSE]
Output
y1 y2 h 2 7 m 2 11 e 3 8 i 3 14 p 3 3 r 3 9 c 4 6 a 5 7 b 5 5 g 5 9 n 5 6 o 5 11 k 6 9 q 6 10 t 6 8 d 7 1 f 7 9 s 7 8 j 12 6 l 12 7
- Related Articles
- How to change the column names and row names of a data frame in R?
- How to remove column names from an R data frame?
- How to remove rows in an R data frame using row names?
- How to remove underscore from column names of an R data frame?
- How to create a subset of a data frame in R without using column names?
- How to combine multiple columns into one in R data frame without using column names?
- How to sort each row of an R data frame in increasing order?
- How to convert a matrix into a data frame with column names and row names as variables in R?
- How to sort a numerical factor column in an R data frame?
- How to convert a matrix to a data frame with column names and row names as new columns in R?
- Find the column and row names in the R data frame based on condition.
- How to remove a common suffix from column names in an R data frame?
- How to create a row sum and a row product column in an R data frame?
- How to deal with missing column for row names when converting data frame to data.table object in R?
- How to change row values based on column values in an R data frame?

Advertisements