我正在寻找一种在保留原始组信息的同时使用group_split()or语法的方法summarise()。我已经看过一些以前的页面,例如此处和此处,使用这些方法,但它们不保留分组信息。有没有办法做到这一点?我当然可以加入数据,但希望避免使用这种方法。
> set.seed(22)\n> # Create fake data\n> flavor <- data.frame(\n+ temperature = sample(x = c(\'hot\',\'cold\'), size = 500, replace = TRUE),\n+ color = sample(c(\'red\',\'blue\',\'green\'), 500, TRUE),\n+ texture = sample(c(\'crumbly\', \'crispy\', \'wet\', \'soft\'), 500, TRUE),\n+ flavor = sample.int(n = 100, size = 500, replace = TRUE)\n+ )\n> \n> head(flavor, 10)\n temperature color texture flavor\n1 cold red soft 47\n2 hot red crumbly 2\n3 cold blue crispy 28\n4 cold blue soft 36\n5 cold blue crumbly 69\n6 cold red soft 49\n7 cold blue soft 100\n8 hot blue crumbly 42\n9 hot blue soft 93\n10 hot green wet 47\nRun Code Online (Sandbox Code Playgroud)\n使用基本分割+映射(有效但不保留原始组信息)
\n> flavor %>%\n+ group_by(color, texture) %>%\n+ mutate(subsets = cur_group_id()) %>%\n+ ungroup() %>%\n+ base::split(.$subsets) %>%\n+ purrr::map(~ wilcox.test(flavor ~ temperature, data = .)) %>%\n+ purrr::map_dfr(~ broom::tidy(.))\n# A tibble: 12 \xc3\x97 4\n statistic p.value method alternative\n <dbl> <dbl> <chr> <chr> \n 1 237 0.687 Wilcoxon rank sum test with continuity correction two.sided \n 2 152. 0.866 Wilcoxon rank sum test with continuity correction two.sided \n 3 236. 0.696 Wilcoxon rank sum test with continuity correction two.sided \n 4 308 0.216 Wilcoxon rank sum test with continuity correction two.sided \n 5 256 0.281 Wilcoxon rank sum test with continuity correction two.sided \n 6 122 0.540 Wilcoxon rank sum test with continuity correction two.sided \n 7 244 0.742 Wilcoxon rank sum test with continuity correction two.sided \n 8 130. 0.0393 Wilcoxon rank sum test with continuity correction two.sided \n 9 238. 0.317 Wilcoxon rank sum test with continuity correction two.sided \n10 360. 0.345 Wilcoxon rank sum test with continuity correction two.sided \n11 75 0.0292 Wilcoxon rank sum test with continuity correction two.sided \n12 219 0.149 Wilcoxon rank sum test with continuity correction two.sided \nThere were 12 warnings (use warnings() to see them)\nRun Code Online (Sandbox Code Playgroud)\n使用类似总结的方法吗?(保留组信息但统计不正确)
\n> flavor %>%\n+ group_by(color, texture) %>%\n+ summarise(output = wilcox.test(flavor ~ temperature, data = .) %>% broom::tidy())\n`summarise()` has grouped output by \'color\'. You can override using the `.groups` argument.\n# A tibble: 12 \xc3\x97 3\n# Groups: color [3]\n color texture output$statistic $p.value $method $alternative\n <chr> <chr> <dbl> <dbl> <chr> <chr> \n 1 blue crispy 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n 2 blue crumbly 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n 3 blue soft 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n 4 blue wet 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n 5 green crispy 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n 6 green crumbly 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n 7 green soft 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n 8 green wet 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n 9 red crispy 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n10 red crumbly 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n11 red soft 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \n12 red wet 30656. 0.721 Wilcoxon rank sum test with continuity correction two.sided \nRun Code Online (Sandbox Code Playgroud)\n使用 group_split (与第一个问题相同)
\n> flavor %>%\n+ group_split(color, texture) %>%\n+ map_dfr(~wilcox.test(flavor ~ temperature, data = .) %>% broom::tidy())\n# A tibble: 12 \xc3\x97 4\n statistic p.value method alternative\n <dbl> <dbl> <chr> <chr> \n 1 237 0.687 Wilcoxon rank sum test with continuity correction two.sided \n 2 152. 0.866 Wilcoxon rank sum test with continuity correction two.sided \n 3 236. 0.696 Wilcoxon rank sum test with continuity correction two.sided \n 4 308 0.216 Wilcoxon rank sum test with continuity correction two.sided \n 5 256 0.281 Wilcoxon rank sum test with continuity correction two.sided \n 6 122 0.540 Wilcoxon rank sum test with continuity correction two.sided \n 7 244 0.742 Wilcoxon rank sum test with continuity correction two.sided \n 8 130. 0.0393 Wilcoxon rank sum test with continuity correction two.sided \n 9 238. 0.317 Wilcoxon rank sum test with continuity correction two.sided \n10 360. 0.345 Wilcoxon rank sum test with continuity correction two.sided \n11 75 0.0292 Wilcoxon rank sum test with continuity correction two.sided \n12 219 0.149 Wilcoxon rank sum test with continuity correction two.sided \nRun Code Online (Sandbox Code Playgroud)\n
您可以使用该rstatix包,该包旨在使用tidyverse.
library(rstatix)
library(tidyverse)
flavor |>
group_by(color, texture) |>
wilcox_test(flavor ~ temperature)
# A tibble: 12 x 9
# color texture .y. group1 group2 n1 n2 statistic p
# * <chr> <chr> <chr> <chr> <chr> <int> <int> <dbl> <dbl>
# 1 blue crispy flavor cold hot 21 21 237 0.687
# 2 blue crumbly flavor cold hot 21 14 152. 0.866
# 3 blue soft flavor cold hot 21 21 236. 0.696
# 4 blue wet flavor cold hot 22 23 308 0.216
# 5 green crispy flavor cold hot 26 24 256 0.281
# 6 green crumbly flavor cold hot 20 14 122 0.54
# 7 green soft flavor cold hot 23 20 244 0.742
# 8 green wet flavor cold hot 20 21 130. 0.0393
# 9 red crispy flavor cold hot 25 23 238. 0.317
#10 red crumbly flavor cold hot 23 27 360. 0.345
#11 red soft flavor cold hot 16 17 75 0.0292
#12 red wet flavor cold hot 18 19 219 0.149
Run Code Online (Sandbox Code Playgroud)