我有一张下面的数据。我希望始终按照这个顺序获得数据,但是当我试图使用id重新排序数据时,它会发生改变,但我不想这样做。有什么解决办法吗?
library(dplyr)
set.seed(10)
df <- data.frame(id = paste(2022,1:20, sep = "-"), weight = round(rnorm(20, 5, 1)))
df
id weight
1 2022-1 5
2 2022-2 5
3 2022-3 4
4 2022-4 4
5 2022-5 5
6 2022-6 5
7 2022-7 4
8 2022-8 5
9 2022-9 3
10 2022-10 5
11 2022-11 6
12 2022-12 6
13 2022-13 5
14 2022-14 6
15 2022-15 6
16 2022-16 5
17 2022-17 4
18 2022-18 5
19 2022-19 6
20 2022-20 5wrong_order_df <- df %>% arrange(weight) %>% arrange(id)
wrong_order_df
id weight
1 2022-1 5
2 2022-10 5
3 2022-11 6
4 2022-12 6
5 2022-13 5
6 2022-14 6
7 2022-15 6
8 2022-16 5
9 2022-17 4
10 2022-18 5
11 2022-19 6
12 2022-2 5
13 2022-20 5
14 2022-3 4
15 2022-4 4
16 2022-5 5
17 2022-6 5
18 2022-7 4
19 2022-8 5
20 2022-9 3我提出的想法是添加一个新的专栏,只是为了解决这个问题。但我相信还有一种更隐秘的方法。
correct_order_df <- wrong_order_df %>% mutate(id_order = as.numeric(str_extract(id, '\\b\\w+$'))) %>% arrange(id_order)
correct_order_df
id weight id_order
1 2022-1 5 1
2 2022-2 5 2
3 2022-3 4 3
4 2022-4 4 4
5 2022-5 5 5
6 2022-6 5 6
7 2022-7 4 7
8 2022-8 5 8
9 2022-9 3 9
10 2022-10 5 10
11 2022-11 6 11
12 2022-12 6 12
13 2022-13 5 13
14 2022-14 6 14
15 2022-15 6 15
16 2022-16 5 16
17 2022-17 4 17
18 2022-18 5 18
19 2022-19 6 19
20 2022-20 5 20发布于 2022-07-13 03:25:12
您可以在安排内输入规格。不知道我们需要多优雅的一句台词。如果这有用的话,请告诉我:
arrange(df, as.numeric(str_extract(id, "(?<=-)\\d+")))https://stackoverflow.com/questions/72960488
复制相似问题