如何在 R 中使用 purrr 应用变量来计算它们
How to apply variables to calcute them using purrr in R
这是我的部分数据
df <- data.frame(
Group1 = rep(c('A', 'A'), 50),
Group2 = rep(c('A', 'B'), 50),
Group3 = rep(c('A', 'B'), 50),
Value1 = rnorm(50,12,4),
Value2 = rnorm (50,10,4),
Value3 = rnorm (50,10,4)
)
Group1对应value1,Group2对应Value2,Group3对应Value 3。我想用t.test得到a table( 不是列表) 每个使用 Purrr 的组的均值和 p 值。
我使用 purrr 使用了以下代码。
df %>%
split.default(rep_len(1:3, ncol(.))) %>%
pmap(~t.test(.x,.y))
使用 tidyverse
,我们可以使用 pivot_longer
重塑为 'long',然后执行 t.test
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = everything(), names_to = c( ".value", "grp"),
names_pattern = "(\D+)(\d+)") %>%
group_by(grp) %>%
summarise(out = if(n_distinct(Group) > 1)
list(t.test(Value ~ Group) %>% broom::tidy(.)) else list(NULL)) %>%
unnest_wider(out)
-输出
# A tibble: 3 × 11
grp estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 1 NA NA NA NA NA NA NA NA <NA> <NA>
2 2 -0.0796 10.8 10.9 -0.114 0.909 98.0 -1.46 1.30 Welch Two Sample t-test two.sided
3 3 -1.42 8.53 9.96 -2.14 0.0350 94.6 -2.75 -0.102 Welch Two Sample t-test two.sided
或使用 purrr
library(purrr)
library(stringr)
df %>%
split.default(str_remove(names(.), "\D+")) %>%
map_dfr(~ .x %>%
rename_with(~ str_remove(.x, "\d+"), everything()) %>%
{if(n_distinct(.x$Group) > 1) t.test(Value ~ Group, data = .)
} %>%
broom::tidy(.), .id = "grp")
-输出
# A tibble: 2 × 11
grp estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 2 -0.0796 10.8 10.9 -0.114 0.909 98.0 -1.46 1.30 Welch Two Sample t-test two.sided
2 3 -1.42 8.53 9.96 -2.14 0.0350 94.6 -2.75 -0.102 Welch Two Sample t-test two.sided
这是我的部分数据
df <- data.frame(
Group1 = rep(c('A', 'A'), 50),
Group2 = rep(c('A', 'B'), 50),
Group3 = rep(c('A', 'B'), 50),
Value1 = rnorm(50,12,4),
Value2 = rnorm (50,10,4),
Value3 = rnorm (50,10,4)
)
Group1对应value1,Group2对应Value2,Group3对应Value 3。我想用t.test得到a table( 不是列表) 每个使用 Purrr 的组的均值和 p 值。
我使用 purrr 使用了以下代码。
df %>%
split.default(rep_len(1:3, ncol(.))) %>%
pmap(~t.test(.x,.y))
使用 tidyverse
,我们可以使用 pivot_longer
重塑为 'long',然后执行 t.test
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = everything(), names_to = c( ".value", "grp"),
names_pattern = "(\D+)(\d+)") %>%
group_by(grp) %>%
summarise(out = if(n_distinct(Group) > 1)
list(t.test(Value ~ Group) %>% broom::tidy(.)) else list(NULL)) %>%
unnest_wider(out)
-输出
# A tibble: 3 × 11
grp estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 1 NA NA NA NA NA NA NA NA <NA> <NA>
2 2 -0.0796 10.8 10.9 -0.114 0.909 98.0 -1.46 1.30 Welch Two Sample t-test two.sided
3 3 -1.42 8.53 9.96 -2.14 0.0350 94.6 -2.75 -0.102 Welch Two Sample t-test two.sided
或使用 purrr
library(purrr)
library(stringr)
df %>%
split.default(str_remove(names(.), "\D+")) %>%
map_dfr(~ .x %>%
rename_with(~ str_remove(.x, "\d+"), everything()) %>%
{if(n_distinct(.x$Group) > 1) t.test(Value ~ Group, data = .)
} %>%
broom::tidy(.), .id = "grp")
-输出
# A tibble: 2 × 11
grp estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 2 -0.0796 10.8 10.9 -0.114 0.909 98.0 -1.46 1.30 Welch Two Sample t-test two.sided
2 3 -1.42 8.53 9.96 -2.14 0.0350 94.6 -2.75 -0.102 Welch Two Sample t-test two.sided