Reputation: 407
I quickly learned that arrange() from {dplyr} is a better for my analysis than sort(). Just curious it is because the object returned by sort() function is of numeric class. Example:
df <- data.frame(a=round(rpois(10,10)), b=LETTERS[1:10], stringsAsFactors = F)
df
a b
1 13 A
2 7 B
3 11 C
4 6 D
5 10 E
6 10 F
7 10 G
8 6 H
9 11 I
10 9 J
I found out that arrange() is good at rearranging if I want to sort the data frame.
arrange(df, desc(a))
a b
1 13 A
2 11 C
3 11 I
4 10 E
5 10 F
6 10 G
7 9 J
8 7 B
9 6 D
10 6 H
If I try to order the original data frame, what is sort doing?
df[sort(df[,1]),]
a b
6 10 F
6.1 10 F
7 10 G
9 11 I
10 9 J
10.1 9 J
10.2 9 J
NA NA <NA>
NA.1 NA <NA>
NA.2 NA <NA>
So I am think it is generally not a good idea to rearrange with a numeric vector.
sort(df[,1])
[1] 6 6 7 9 10 10 10 11 11 13
class(sort(df[,1]))
[1] "numeric"
I would like to know what happened when I tried to rearrange with sort(). Thanks.
Upvotes: 1
Views: 53
Reputation: 887118
We can use order
to provide the index.
df[order(-df$a),]
If we are using sort
, use the index.return
option as TRUE
df[sort(df$a, decreasing = TRUE, index.return = TRUE)$ix,]
Upvotes: 2