【发布时间】:2021-01-02 14:44:16
【问题描述】:
我有这个带有可重现数据的 df:
structure(list(`Loperamida en diarrea` = structure(c(1L, 1L,
1L, 2L, 4L, 3L, 4L, 1L, 2L, 1L), .Label = c("muy efectiva", "algo efectiva",
"no efectiva", "no se"), class = c("ordered", "factor")), `Carbón en diarrea` = structure(c(2L,
2L, 2L, 4L, 4L, 3L, 4L, 3L, 3L, 4L), .Label = c("muy efectiva",
"algo efectiva", "no efectiva", "no se"), class = c("ordered",
"factor")), `Bismuto en diarrea` = structure(c(2L, 1L, 2L, 4L,
3L, 3L, 2L, 2L, 2L, 1L), .Label = c("muy efectiva", "algo efectiva",
"no efectiva", "no se"), class = c("ordered", "factor")), `Rifaximina en diarrea` = structure(c(2L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L), .Label = c("muy efectiva",
"algo efectiva", "no efectiva", "no se"), class = c("ordered",
"factor")), `Otros antibióticos en diarrea` = structure(c(2L,
1L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 4L), .Label = c("muy efectiva",
"algo efectiva", "no efectiva", "no se"), class = c("ordered",
"factor")), `Probióticos en diarrea` = structure(c(2L, 2L, 2L,
2L, 2L, 1L, 2L, 3L, 3L, 2L), .Label = c("muy efectiva", "algo efectiva",
"no efectiva", "no se"), class = c("ordered", "factor")), `Orientación dicotómica` = c("Neurogastro",
"Neurogastro", "Neurogastro", "No neurogastro", "Neurogastro",
"Neurogastro", "Neurogastro", "No neurogastro", "No neurogastro",
"No neurogastro")), row.names = c(NA, 10L), class = "data.frame")
我创建了一个数据框,通过使用以下代码旋转 df 来计算分类观察:
library(tidyverse)
library(janitor)
df %>%
pivot_longer(cols = everything()) %>%
count(name, value) %>%
pivot_wider(names_from = value, values_from = n, values_fill = 0) %>%
mutate("efectiva" = `algo efectiva` + `muy efectiva`) %>%
arrange(desc(`efectiva`)) %>%
select(c(`name`,`efectiva`, `no efectiva`)) %>%
adorn_percentages("row") %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns()
df$name <- str_remove(df$name, " en diarrea")
结果如下所示:
name efectiva no efectiva
Rifaximina 100.0% (10) 0.0% (0)
Probióticos 80.0% (8) 20.0% (2)
Bismuto 77.8% (7) 22.2% (2)
Loperamida 87.5% (7) 12.5% (1)
Otros antibióticos 77.8% (7) 22.2% (2)
Carbón 50.0% (3) 50.0% (3)
Orientación dicotómica - (0) - (0)
我一直在尝试通过变量 Orientación dicotómica(Neurogastro 与 No Neurogastro) 来分隔列,但我无法对其进行排序。我期望的是这样的:
Neurogastro No neurogastro
name efectiva no efectiva efectiva no efectiva
Rifaximina 98.1% (52) 1.9% (1) 96.4% (240) 3.6% (9)
Dieta 98.1% (51) 1.9% (1) 91.6% (229) 8.4% (21)
Trimebutina 96.0% (48) 4.0% (2) 86.3% (214) 13.7% (34)
Amitriptilina 97.8% (45) 2.2% (1) 88.8% (214) 11.2% (27)
Trimebutina/simeticona 88.2% (45) 11.8% (6) 84.0% (205) 16.0% (39)
Antiespasmódicos 93.6% (44) 6.4% (3) 81.4% (184) 18.6% (42)
有什么建议吗?
【问题讨论】:
-
什么是
df_count?请为adorn_percentages、adorn_pct_formatting、adorn_ns指定库? -
您的
df包含 10 行并且示例输出显示超过 10 行?你在这里错过了什么???洛哌丁胺没有行吗? -
抱歉耽搁了@AnilGoyal。我
ve seen Id 忘记了库,但您已经编辑了问题。装饰函数在 janitor 包中,pivot 在 tidyverse 中。 -
我在 dput() 中使用了 df 的缩短版本,因此它与我发布的不匹配。我已经编辑了输出,使其与数据匹配。最后一个输出只是一个例子,不要管值。
标签: r dataframe join merge janitor