R在表格中添加元素数量

时间:2019-02-27 12:04:02

标签: r t-test

我想在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

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

2 个答案:

答案 0 :(得分:0)

一种选择是通过创建函数来预先存储'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,输出值将有所不同

答案 1 :(得分:0)

由于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中,这样您就可以从元素的长度中检索信息。