I would like to order rows within a data frame according to different columns and I don't know how I could achieve this. Here an example to clarify:
x <- data.frame(X=c(10:1), Y=c(6,6,3,6,3,3,9,9,9,2), L=c("A","B","C","C","A","B","C","A","B","C"))
x
X Y L
1 10 6 A
2 9 6 B
3 8 3 C
4 7 6 C
5 6 3 A
6 5 3 B
7 4 9 C
8 3 9 A
9 2 9 B
10 1 2 C
to
X Y L
1 3 9 A
2 6 3 A
3 10 6 A
4 9 6 B
5 5 3 B
6 2 9 B
7 1 2 C
8 8 3 C
9 7 6 C
10 4 9 C
In this example, the values that are "A" are ordered by increasing "X". The values that have "B" are ordered by decreasing "X" and values with "C" are ordered by increasing "Y".
Can this be done with order()
or %>% arrange
?
(data frame is large)
Thanks!
You can create a new vector with the conditions which is used by
order
.