# 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 Live Demo 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

Live Demo

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