【问题标题】:Store or print results for 't.test' in for loop在 for 循环中存储或打印“t.test”的结果
【发布时间】:2013-02-28 12:17:10
【问题描述】:

我是 R 新手,在 R 中打印“for”循环的结果时遇到问题。这是我的代码:

afile <- read.table(file = 'data.txt', head =T)##Has three columns Lab, Store and Batch
lab1 <- afile$Lab[afile$Batch == 1]
lab2 <- afile$Lab[afile$Batch == 2]
lab3 <- afile$Lab[afile$Batch == 3]
lab_list <- list(lab1,lab2,lab3)
for (i in 1:2){
    x=lab_list[[i]]
    y=lab_list[[i+1]]
    t.test(x,y,alternative='two.sided',conf.level=0.95)
 }

此代码运行没有任何错误,但在屏幕上没有输出。我尝试使用“分配”将结果放入变量中,但这会产生错误:

for (i in 1:2){x=lab_list[[i]];y=lab_list[[i+1]];assign(paste(res,i,sep=''),t.test(x,y,alternative='two.sided',conf.level=0.95))}
Warning messages:
1: In assign(paste(res, i, sep = ""), t.test(x, y, alternative = "two.sided",  :
only the first element is used as variable name
2: In assign(paste(res, i, sep = ""), t.test(x, y, alternative = "two.sided",  :
only the first element is used as variable name

请帮助我了解如何在循环中执行 t.test 并获得结果,即在屏幕上打印或保存在变量中。

AK

【问题讨论】:

    标签: r for-loop


    【解决方案1】:

    我会这样重写你的代码:

    我假设你的数据是这样的

    afile <- data.frame(Batch= sample(1:3,10,rep=TRUE),lab=rnorm(10))
    afile
       Batch        lab
    1      2  0.4075675
    2      1  0.3006192
    3      1 -0.4824655
    4      3  1.0656481
    5      1  0.1741648
    6      2 -1.4911526
    7      2  0.2216970
    8      1 -0.3862147
    9      1 -0.4578520
    10     1 -0.6298040
    

    然后使用lapply,您可以将结果存储在列表中:

    lapply(1:2,function(i){
     x <- subset(afile,Batch==i)
     y <- subset(afile,Batch==i+1)
       t.test(x,y,alternative='two.sided',conf.level=0.95)
    })
    
    [[1]]
    
        Welch Two Sample t-test
    
    data:  x and y 
    t = -0.7829, df = 6.257, p-value = 0.4623
    alternative hypothesis: true difference in means is not equal to 0 
    95 percent confidence interval:
     -1.964637  1.005008 
    sample estimates:
    mean of x mean of y 
    0.3765373 0.8563520 
    
    
    [[2]]
    
        Welch Two Sample t-test
    
    data:  x and y 
    t = -1.0439, df = 1.797, p-value = 0.4165
    alternative hypothesis: true difference in means is not equal to 0 
    95 percent confidence interval:
     -6.588720  4.235776 
    sample estimates:
    mean of x mean of y 
     0.856352  2.032824 
    

    【讨论】:

      【解决方案2】:

      在很多情况下,您需要在循环中显式打印结果。试试:

      print(t.test(x,y,alternative='two.sided',conf.level=0.95))
      

      print(summary(t.test(x,y,alternative='two.sided',conf.level=0.95)))
      

      【讨论】:

      • 如何将这些打印结果存储到新变量中?
      • 你需要一个特定的结构来包含你的结果,否则每个结果都会随着循环的推进而被覆盖。这个answer 说明了一种方法。 t.test 返回一个列表,因此您可能需要创建一个列表列表或只选择您想要的数据,并将其保存在 data.frame 或类似的东西中。或者,更好的是,看看下面这个answer
      【解决方案3】:

      除了“汉森”打印解决方案外,还可以保存和打印结果,如下所示:

      result <- vector("list",6)
      for (i in 1:5){x=lab_list[[i]];y=lab_list[[i+1]];result[[i]] = t.test(x,y,alternative='two.sided',conf.level=0.95)}
      result
      

      AK

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-19
        • 1970-01-01
        • 1970-01-01
        • 2022-08-03
        相关资源
        最近更新 更多