如何使用tidyverse将表格扩展为更宽,如果某些因子水平为空

huangapple go评论73阅读模式
英文:

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"))

某些组合的命名变量 periodolugar 的一些水平是空的,在 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(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, 
&quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;), nihss = structure(c(2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 
6L), levels = c(&quot;0&quot;, &quot;1-4&quot;, &quot;5-15&quot;, &quot;16-20&quot;, &quot;20+&quot;, &quot;9999&quot;), class = &quot;factor&quot;), 
    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 = &quot;data.frame&quot;) 

One column, nihss, is a factor variable, with ordered levels:

stroke$nihss &lt;- factor(stroke$nihss, levels = c(&quot;0&quot;,&quot;1-4&quot;,&quot;5-15&quot;,&quot;16-20&quot;,&quot;20+&quot;,&quot;9999&quot;))

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 %&gt;% 
  pivot_wider(names_from = c(lugar, periodo), values_from = porcent, values_fill = 0) %&gt;% 
  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:

&quot;0&quot;,&quot;1-4&quot;,&quot;5-15&quot;,&quot;16-20&quot;,&quot;20+&quot;,&quot;9999&quot;

答案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 &lt;- factor(stroke$nihss, levels = c(&quot;0&quot;,&quot;1-4&quot;,&quot;5-15&quot;,&quot;16-20&quot;,&quot;20+&quot;,&quot;9999&quot;))

 stroke |&gt;
   tidyr::pivot_wider(names_from = c(lugar, periodo), values_from = porcent, values_fill = 0) |&gt;
   dplyr::arrange(nihss) |&gt;
   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>

huangapple
  • 本文由 发表于 2023年6月29日 03:16:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定