【问题标题】:R Add number of elements in a tableR在表格中添加元素数
【发布时间】:2019-02-27 12:04:02
【问题描述】:

我想在 t 检验表中添加值的数量。这是我的示例代码:

library(broom)
library(purrr)

t1 <- t.test(rnorm(50), rnorm(60))
t2 <- t.test(rnorm(60), rnorm(70, 1))
t3 <- t.test(rnorm(80), rnorm(90, 2))

现在我用 broom 和 purrr 包将它们变成一个数据框(然后可以打印为表格),我得到了这张表格:

tab <- map_df(list(t1, t2, t3), tidy)
tab %>% select(-parameter, -conf.low, -conf.high, -method, -alternative)

# A tibble: 3 x 5
estimate estimate1 estimate2 statistic  p.value
 <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
1  -0.0542    -0.178    -0.123    -0.260 7.95e- 1
2  -1.24      -0.214     1.03     -6.48  1.88e- 9
3  -2.30      -0.231     2.07    -14.6   2.81e-31

现在我想添加 2 个新列,其中包含 x 的数量和 y 的数量。 这是我想要的输出:

# A tibble: 3 x 5
  estimate estimate1 estimate2 statistic  p.value  number_of_x  number_of_Y
 <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
1  -0.0542    -0.178    -0.123    -0.260 7.95e- 1      50          60
2  -1.24      -0.214     1.03     -6.48  1.88e- 9      60          70
3  -2.30      -0.231     2.07    -14.6   2.81e-31      80          90

有人可以帮我创建这个决赛桌吗?

【问题讨论】:

    标签: r t-test


    【解决方案1】:

    由于t.test 不返回有关xy 长度的信息,因此您必须将其保存在运行t.test 的位置:

    library(tidyverse)
    
    params <- list(xn  = c(50, 60, 80),
                   xmu = rep(0, 3),
                   yn  = c(60, 70, 90),
                   ymu = 0:2)
    
    raw_data <- pmap(params, function(xn, xmu, yn, ymu) list(x = rnorm(xn, xmu),
                                                             y = rnorm(yn, ymu)))
    
    map_dfr(raw_data, function(l) {
      tt <- t.test(l$x, l$y)
      tidy(tt) %>% 
         select(-parameter, -conf.low, -conf.high, -method, -alternative) %>%
         mutate(number_of_x = length(l$x),
                number_of_y = length(l$y))
    }) 
    # A tibble: 3 x 7
    #   estimate estimate1 estimate2 statistic  p.value number_of_x number_of_y
    #      <dbl>     <dbl>     <dbl>     <dbl>    <dbl>       <int>       <int>
    # 1    0.260    0.144     -0.115      1.38 1.71e- 1          50          60
    # 2   -1.16    -0.0414     1.12      -6.24 6.25e- 9          60          70
    # 3   -1.67     0.129      1.79     -10.6  4.14e-20          80          90
    

    注意。为了在循环中执行此操作,我将所有输入向量存储在raw_data 中,这样您就可以从元素的长度中检索信息。

    【讨论】:

      【解决方案2】:

      一个选项是通过创建一个函数预先存储“n”

      library(tidyverse)
      f1 <- function(n1, n2, mean1 = 1) {
      
       list(t.test(rnorm(n1), rnorm(n2, mean = mean1)),
           tibble(number_of_x = n1, number_of_y = n2))
      
       }
      
      t1 <- f1(50, 60)
      t2 <- f1(60, 70, 1)
      t3 <- f1(80, 90, 2)
      map2_df(list(t1[[1]], t2[[1]], t3[[1]]), list(t1[[2]],
             t2[[2]], t3[[2]]), ~ 
         tidy(.x) %>%
               select(-parameter, -conf.low, -conf.high, -method, -alternative) %>%
               bind_cols(.y))
      # A tibble: 3 x 7
      #  estimate estimate1 estimate2 statistic  p.value number_of_x number_of_y
      #     <dbl>     <dbl>     <dbl>     <dbl>    <dbl>       <dbl>       <dbl>
      #1   -0.723    0.348       1.07     -3.77 2.67e- 4          50          60
      #2   -1.25    -0.216       1.03     -7.32 2.54e-11          60          70
      #3   -2.07     0.0433      2.11    -13.0  3.01e-27          80          90
      

      或使用pmap

      list(t1, t2, t3) %>% 
           transpose %>% 
           pmap_df(~ tidy(.x) %>% 
              select(-parameter, -conf.low, -conf.high, -method, -alternative) %>%
              bind_cols(.y))
      

      注意:输出值会有所不同,因为没有指定 set.seed

      【讨论】:

      • 我猜在您的f1 中您应该更改为rnorm(n2, mean = sd1) 以模仿OP 的示例,特别是因为2 样本t 检验的假设之一是方差相等;)
      • @thothal 谢谢,有道理
      猜你喜欢
      • 2018-11-17
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多