R 在表中添加数据帧的名称

R 在表中添加数据帧的名称,r,purrr,R,Purrr,我想在t-tests表中添加数据帧的名称。 下面是我的示例代码: t1 <- t.test(rnorm(100), rnorm(100) t2 <- t.test(rnorm(100), rnorm(100, 1)) t3 <- t.test(rnorm(100), rnorm(100, 2)) 最后,我想在第一列中添加数据帧(t1、t2、t3)的名称。有人能解释一下我是如何做到这一点的吗?我们可以先设置名称,然后使用.id参数添加列 set.seed(123) t1 &l

我想在t-tests表中添加数据帧的名称。 下面是我的示例代码:

t1 <- t.test(rnorm(100), rnorm(100)
t2 <- t.test(rnorm(100), rnorm(100, 1))
t3 <- t.test(rnorm(100), rnorm(100, 2))

最后,我想在第一列中添加数据帧(t1、t2、t3)的名称。有人能解释一下我是如何做到这一点的吗?

我们可以先
设置名称
,然后使用
.id
参数添加列

set.seed(123)

t1 <- t.test(rnorm(100), rnorm(100))
t2 <- t.test(rnorm(100), rnorm(100, 1))
t3 <- t.test(rnorm(100), rnorm(100, 2))

library(broom)
library(purrr)

tab <- list(t1, t2, t3) %>%
  set_names(paste0("t", 1:3)) %>%
  map_dfr(tidy, .id = "Test")
tab
# # A tibble: 3 x 11
#   Test  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 t1       0.198    0.0904    -0.108      1.49 1.38e- 1      197.  -0.0643     0.460 Welch Two Samp~ two.sided  
# 2 t2      -0.843    0.120      0.964     -5.99 9.79e- 9      196.  -1.12      -0.566 Welch Two Samp~ two.sided  
# 3 t3      -1.85     0.106      1.96     -13.6  4.31e-30      197.  -2.12      -1.58  Welch Two Samp~ two.sided
set.seed(123)
t1您可以使用
ls()
和一个简单的正则表达式来捕获您的测试,即

v1 <- ls()[grepl('^t[0-9]+', ls())]
#[1] "t1" "t2" "t3"

v1使用命名列表,您将在结果表中获得名称:

ll <- list(t1 = t1, t2 = t2, t3 = t3)
tab <-
  ll %>%
  map_df(tidy, .id = "id")
ll
v1 <- ls()[grepl('^t[0-9]+', ls())]
#[1] "t1" "t2" "t3"
ll <- list(t1 = t1, t2 = t2, t3 = t3)
tab <-
  ll %>%
  map_df(tidy, .id = "id")