鑑於以下data.frame,我想要爲每個組的數字的順序。dplyr與命令()產生錯誤的訂單
df
group numbers
1 A -0.80097537
2 B -0.69498701
3 C 0.55627105
4 D -0.05810593
5 A -1.41748489
6 B 0.30198594
7 C 1.11918243
8 D 0.02595183
9 A 1.74417489
10 B 0.42435785
11 C 0.75889049
12 D -2.21025222
13 A 0.57149543
14 B 0.77944238
15 C 3.04021182
16 D -0.14157181
17 A -0.29213733
18 B -1.00858701
19 C 1.49959112
20 D -0.57532183
structure(list(group = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), class = "factor", .Label = c("A",
"B", "C", "D")), numbers = c(-0.800975371801324, -0.694987011133934,
0.556271051640264, -0.0581059266921911, -1.41748489222262, 0.301985943949874,
1.11918243487368, 0.0259518302570701, 1.74417489077084, 0.424357848275249,
0.758890492984891, -2.2102522179535, 0.571495432426037, 0.779442380219119,
3.04021182328692, -0.141571814386413, -0.292137333159453, -1.00858701158259,
1.49959111842538, -0.575321833031783)), .Names = c("group", "numbers"
), row.names = c(NA, -20L), class = "data.frame")
不知何故爲組d的順序不正確:
df %>%
group_by(group) %>%
mutate(x=order(numbers)) %>%
arrange(group, x)
Source: local data frame [20 x 3]
Groups: group [4]
group numbers x
<fctr> <dbl> <int>
1 A -1.41748489 1
2 A -0.80097537 2
3 A -0.29213733 3
4 A 0.57149543 4
5 A 1.74417489 5
6 B 0.30198594 1
7 B 0.42435785 2
8 B 0.77944238 3
9 B -1.00858701 4
10 B -0.69498701 5
11 C 0.55627105 1
12 C 0.75889049 2
13 C 1.11918243 3
14 C 1.49959112 4
15 C 3.04021182 5
16 D -0.14157181 1
17 D -0.57532183 2
18 D -0.05810593 3
19 D -2.21025222 4
20 D 0.02595183 5
因此具體地,線19的順序是錯誤的。任何關於我的誤解的想法?
'df%>%group_by(group)%>%arrange(group,numbers)'給出正確的輸出。你可以使用'dense_rank'而不是'order',即'df%>%group_by(group)%>%mutate(x = dense_rank(numbers))%>%arrange(group,x)' – akrun
但是爲什麼order和dense_rank給出不同的結果? – c0bra
只要做到這一點'%>%group_by(group)%>%mutate(x = order(order(numbers)))%>%arrange(group,x)' – akrun