sort.df <- function(d)
{
## Purpose: Sort a Data Frame along 1st column, ties along 2nd, ..., until its last column.
## Arguments:
## d: a data frame
## Return: a sorted data frame
## Author: Feiming Chen, Date: 18 Apr 2017, 13:49
## ________________________________________________
d[ do.call(order, d), ]
}
if (F) { # Unit Test
df <- data.frame(X = c("b", "a", "a", "c"), Y = c(4, 2, 1, 3))
sort.df(df)
## X Y
## 3 a 1
## 2 a 2
## 1 b 4
## 4 c 3
sort.df(df[2:1])
## Y X
## 3 1 a
## 2 2 a
## 4 3 c
## 1 4 b
}
Tuesday, April 18, 2017
Sort a Data Frame
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment