将数据帧的许多列的t.test的结果(p值)转换为具有每列名称信息的数据帧

时间:2018-04-14 06:37:38

标签: r dataframe

在数据框的几个列(标题名称:q1,q2,q3)上执行t.test后,结果如下所示:

$q1  

Welch Two Sample t-test  

data:  i by d$group  
t = -0.76262, df = 17.323, p-value = 0.4559  
alternative hypothesis: true difference in means is not equal to 0  
95 percent confidence interval:  
 -1.2294678  0.5759458  
sample estimates:  
mean in group A mean in group B   
    -0.05443279      0.27232820   


$q2  

Welch Two Sample t-test  

data:  i by d$group  
t = -1.6467, df = 17.731, p-value = 0.1172  
alternative hypothesis: true difference in means is not equal to 0  
95 percent confidence interval:  
 -1.2881952  0.1568201  
sample estimates:  
mean in group A mean in group B   
     -0.3906697       0.1750179   


$q3  

Welch Two Sample t-test  

data:  i by d$group  
t = 0.52889, df = 13.016, p-value = 0.6058  
alternative hypothesis: true difference in means is not equal to 0  
95 percent confidence interval:  
 -0.7569843  1.2478547  
sample estimates:  
mean in group A mean in group B   
    0.253746354     0.008311147   

我想要做的是获取单个p值,并形成如下数据框或矩阵:

   (the column name) q1 q2 q3  
   (the p-value) 0.4559 0.1172 0.6058

我尝试将t.test结果(列表)保存为d_df_ttest,然后使用
for loop like:

for(v in 1:length(d_df_ttest)) {   
print (d_df_ttest[[v]]$p.value)  
}

但我只能得到:

-[1] 0.4559469  
-[1] 0.1172263  
-[1] 0.6057874 

请您帮我一个包含原始列名(q1,q2,q3)和相应p值的数据框吗?

非常感谢,

杰夫

2 个答案:

答案 0 :(得分:1)

创建一个空矩阵,然后用

填充它
# Create data
set.seed(123)  # This makes sampling replicable
df <- data.frame(
  q1 = rnorm(20),
  q2 = rnorm(20),
  q3 = rnorm(20),
  group = sample(c("A", "B"), size = 20, replace = TRUE)
)

pval = matrix(NA, ncol = ncol(df)-1, nrow = 1, dimnames = list("p-value",colnames(df)[-4]))
for(i in 1:(ncol(df)-1)){   pval[,i] <- t.test(df[,i]~df$group)$p.value}
pval
              q1        q2        q3
p-value 0.4559469 0.1172263 0.6057874

答案 1 :(得分:1)

我们可以使用#include <thread> #include <condition_variable> #include <iostream> bool flag = false; std::mutex g_mutex; std::condition_variable cv; void threadProc() { std::unique_lock<std::mutex> lck(g_mutex); while (true) { static int count = 0; std::cout << "wait for flag" << ++count << std::endl; cv.wait(lck, []() {return flag; }); // !!!It will blocked at the second round std::cout << "flag is true " << count << std::endl; flag = false; lck.unlock(); } } int main(int argc, char *argv[]) { std::thread t(threadProc); while (true) { static int count = 0; { std::lock_guard<std::mutex> guard(g_mutex); // !!!It will blocked at the second round flag = true; std::cout << "set flag " << ++count << std::endl; } cv.notify_one(); std::this_thread::sleep_for(std::chrono::seconds(1)); } t.join(); return 0; }

执行此操作
summarise_at

library(dplyr) d %>% summarise_at(vars(matches("q\\d+")), funs(t.test(.~ group)$p.value)) # q1 q2 q3 #1 0.4559469 0.1172263 0.6057874

base R

数据

sapply(d[1:3], function(x) t.test(x ~ d$group)$p.value)
#       q1        q2        q3 
# 0.4559469 0.1172263 0.6057874