当名称重复时,您可以对数据框的行进行排序吗?我有一个非常大的数据集,我无法重命名所有重复的变量,它看起来像这样
统计 | 价值 |
---|---|
c | 3 |
d | 7 |
a | 9 |
b | 5 |
b | 1 |
c | 5 |
e | 8 |
f | 5 |
在导出为 Excel 文件之前,我必须对行及其相应的值进行排序(即我需要具有精确格式的 Excel)。
统计 | 价值 |
---|---|
a | 9 |
b | 1 |
c | 3 |
d | 7 |
b | 5 |
c | 5 |
e | 8 |
f | 5 |
数据:
xyzzy = structure(list(stat = c("c", "d", "a", "b", "b", "c", "e", "f"
), value = c(3L, 7L, 9L, 5L, 1L, 5L, 8L, 5L)), class = "data.frame", row.names = c(NA,
-8L))
如果我理解的话,想要的顺序是每个统计数据第一次出现,然后是每个统计数据的下一个出现。
library(dplyr)
df <- tibble(stat = c("c", "d", "a", "b", "b", "e", "c"), value = c(seq(7, 1)))
df <- df |>
group_by(stat) |>
mutate(order = cumsum(row_number())) |>
arrange(order, stat) |>
select(-order)
来自
stat value
<chr> <int>
1 c 7
2 d 6
3 a 5
4 b 4
5 b 3
6 e 2
7 c 1
致:
stat value
<chr> <int>
1 a 5
2 b 4
3 c 7
4 d 6
5 e 2
6 b 3
7 c 1
如果第 5 行所需输出的不一致(如@metehanGungor 指出)只是一个意外,您可以尝试以下操作:
libray(tidyverse)
aux %>%
arrange(value) %>%
mutate(.by = stat, pos = row_number()) %>%
arrange(pos, stat) %>%
select(-pos)
> df
# A tibble: 8 × 2
stat value
<chr> <int>
1 a 9
2 b 1
3 c 3
4 d 7
5 e 8
6 f 5
7 b 5
8 c 5