Reputation: 2654
Suppose I got the following data frame:
A B
1 2
3 4
5 6
And I want to reverse row order in all columns (turn it "upside down"):
A B
5 6
3 4
1 2
I used the following code
sort(df[ , 1:2], decreasing = TRUE)
However, it gives me the following error:
Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = decreasing)) : undefined columns selected
when I specify only one column it works but I need both the columns to be sorted at once.
Upvotes: 3
Views: 6004
Reputation: 99331
You could use rev
to reverse the row names
df[rev(rownames(df)),]
# A B
# 3 5 6
# 2 3 4
# 1 1 2
If you want to correct the new reversed row names, you could write a little function
flip <- function(data) {
new <- data[rev(rownames(data)), ]
rownames(new) <- NULL
new
}
flip(df)
# A B
# 1 5 6
# 2 3 4
# 3 1 2
Upvotes: 7