【问题标题】:How to tidy multiple pairwise.t.tests in R with broom如何用扫帚在 R 中整理多个pairwise.t.tests
【发布时间】:2017-03-24 12:46:34
【问题描述】:

我想知道如何整理以下内容:

首先,我将选择的列收集到一个包含三列的小标题中:应变(=分组因子)、参数(参数名称)和值(实际值)

sel <- t_tcellact %>% select(strain, contains("nbr_")) %>% gather(params, values, nbr_DP:nbr_CD3p)

然后我执行多个pairwise.t.test():

 test2 <- sel %>% bind_rows(sel) %>% split(.$params) %>% map(~ pairwise.t.test(x=.$values, g=.$strain, p.adj = "none"))

结果是pairwise.t.tests的结果列表,我可以开始清理:

test3 <- lapply(test2, tidy)

列表现在看起来像这样:

$nbr_CD3p   

group1 group2        p.value

1    SKG Balb/c 0.000001849548

$nbr_DN_CD69nCD25n  

group1 group2   p.value 

1    SKG Balb/c 0.6295371

等等……

由此我需要一个包含以下列的小标题:参数(例如 nbr_CD3p)、group1、group2、p.value。

在此示例中,我只有两个组,但我想以一种通用方式进行,当我有多个组时也适用。

有没有人知道如何以一种优雅的方式(没有循环)达到这一点?

【问题讨论】:

    标签: r tidyverse broom


    【解决方案1】:

    我找到了一种方法:

    test2 <- sel %>% bind_rows(sel) %>% split(.$params) %>% map(~ pairwise.t.test(x=.$values, g=.$strain, p.adj = "none")) %>% lapply(tidy) %>% do.call("rbind", .) %>% mutate(params = rownames(.)) %>% as_tibble()
    

    【讨论】:

    • 你试过用map_df 代替tidy 代替lapply 吗?如果您使用 .id 参数,这可能会让您一步到位。
    【解决方案2】:

    您应该可以使用bind_rows(),利用 .id 参数:

    test3 <- lapply(test2, tidy) %>%
         bind_rows(.id = 'parameter')
    

    这将使用 test2 的名称作为数据框中名为参数的新列。综上所述,按照上面评论中的 aosmith 建议,将 lapply 替换为 map_df() 也应该有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 2021-11-08
      • 2021-03-29
      • 2019-12-27
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多