英文:
How to pivot wider a table with tidyverse if some factor levels are empty
问题
我想将一个数据框从长格式转换为宽格式。 代码如下:
structure(list(periodo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1), lugar = c("A", "A", "A", "A", "B", "B", "B",
"B", "B", "B", "A", "A", "A", "B", "B", "B", "B", "B"), nihss = structure(c(2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 3L, 4L, 5L, 2L, 3L, 4L, 5L,
6L), levels = c("0", "1-4", "5-15", "16-20", "20+", "9999"), class = "factor"),
porcent = c(5, 56, 17, 22, 1, 12, 53, 15, 18, 1, 30, 30,
40, 6, 37, 27, 28, 2)), row.names = c(NA, -18L), class = "data.frame")
其中一列 nihss
是一个因子变量,具有有序水平:
stroke$nihss <- factor(stroke$nihss, levels = c("0","1-4","5-15","16-20","20+","9999"))
某些组合的命名变量 periodo
和 lugar
的一些水平是空的,在 pivot_wider
中。
我用于转换这些数据的代码是:
stroke %>%
pivot_wider(names_from = c(lugar, periodo), values_from = porcent, values_fill = 0) %>%
kable()
代码按预期工作,重塑后的表格有四列新列。但是,nihss
的水平未按照原始顺序排序。第一个 ("0") 和最后一个 ("9999") 出现在其余类别之后:
nihss A_0 B_0 A_1 B_1
1-4 5 12 0 6
5-15 56 53 30 37
16-20 17 15 30 27
20+ 22 18 40 28
0 0 1 0 0
9999 0 1 0 2
我希望获得一个转换后的表格,其中因子的水平显示为原始排序。即:
"0","1-4","5-15","16-20","20+","9999"
英文:
I want to reshape a dataframe from long to wide format. The code is:
structure(list(periodo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1), lugar = c("A", "A", "A", "A", "B", "B", "B",
"B", "B", "B", "A", "A", "A", "B", "B", "B", "B", "B"), nihss = structure(c(2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 3L, 4L, 5L, 2L, 3L, 4L, 5L,
6L), levels = c("0", "1-4", "5-15", "16-20", "20+", "9999"), class = "factor"),
porcent = c(5, 56, 17, 22, 1, 12, 53, 15, 18, 1, 30, 30,
40, 6, 37, 27, 28, 2)), row.names = c(NA, -18L), class = "data.frame")
One column, nihss
, is a factor variable, with ordered levels:
stroke$nihss <- factor(stroke$nihss, levels = c("0","1-4","5-15","16-20","20+","9999"))
Some levels are empty for some combinations of the named variables, periodo
and lugar
, in pivot_wider.
My code to pivot these data is:
stroke %>%
pivot_wider(names_from = c(lugar, periodo), values_from = porcent, values_fill = 0) %>%
kable()
The code works as expected, and the reshaped table has four new columns. However, the nihss' levels are not ordered according to the original order. The first ("0") and the last ("9999") appears after the remaining categories:
nihss A_0 B_0 A_1 B_1
1-4 5 12 0 6
5-15 56 53 30 37
16-20 17 15 30 27
20+ 22 18 40 28
0 0 1 0 0
9999 0 1 0 2
I expect to get a pivoted table, where the levels of the factor are shown as originally were ordered. That is:
"0","1-4","5-15","16-20","20+","9999"
答案1
得分: 1
将 dplyr::arrange()
添加到您的输出中,如下所示:
[...]
stroke$nihss <- factor(stroke$nihss, levels = c("0","1-4","5-15","16-20","20+","9999"))
stroke |>
tidyr::pivot_wider(names_from = c(lugar, periodo), values_from = porcent, values_fill = 0) |>
dplyr::arrange(nihss) |>
knitr::kable()
nihss | A_0 | B_0 | A_1 | B_1 |
---|---|---|---|---|
0 | 0 | 1 | 0 | 0 |
1-4 | 5 | 12 | 0 | 6 |
5-15 | 56 | 53 | 30 | 37 |
16-20 | 17 | 15 | 30 | 27 |
20+ | 22 | 18 | 40 | 28 |
9999 | 0 | 1 | 0 | 2 |
创建于2023年6月29日,使用reprex v2.0.2
英文:
Add dplyr::arrange()
to your output, like:
[...]
stroke$nihss <- factor(stroke$nihss, levels = c("0","1-4","5-15","16-20","20+","9999"))
stroke |>
tidyr::pivot_wider(names_from = c(lugar, periodo), values_from = porcent, values_fill = 0) |>
dplyr::arrange(nihss) |>
knitr::kable()
nihss | A_0 | B_0 | A_1 | B_1 |
---|---|---|---|---|
0 | 0 | 1 | 0 | 0 |
1-4 | 5 | 12 | 0 | 6 |
5-15 | 56 | 53 | 30 | 37 |
16-20 | 17 | 15 | 30 | 27 |
20+ | 22 | 18 | 40 | 28 |
9999 | 0 | 1 | 0 | 2 |
<sup>Created on 2023-06-29 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论