从循环中保存结果

时间:2014-08-14 21:53:45

标签: r loops output

我是R的新手。我有一个关于如何将循环中的结果保存到文件的问题。这是一个例子:

# Read in the data, set up variables
sdata<-read.csv("sdata.csv", header=T)
m=ncol(sdata)
x=matrix(0,m,4)
row.names(x) <- variable.names(sdata)
colnames(x) <- c( "Ground", "111d", "125d", "Ground")


for (i in 6:m){ 
Tukey1= HSD.test(lm(sdata[,i] ~ sdata$Medium+sdata$color+sdata$type+sdata$Micro), 'sdata$Micro')
x[i,] = Tukey1$goups[1:4,3]

}

我不知道如何使用循环获取所有数据,所以我只尝试了其中一个,

Tukey1 = HSD.test(lm(sdata[,1] ~ sdata$Medium+sdata$color+sdata$type+sdata$Micro), 'sdata$Micro')
Tukey1

结果如下:

$statistics
      Mean       CV   MSerror       HSD r.harmonic
  11.87421 3.102479 0.1357148 0.5288771   7.384615

$parameters
  Df ntr StudentizedRange
  24   4         3.901262

$means
       sdata_mg[, 6]       std  r      Min      Max
111d                  11.86369 0.5317421  6 11.08623 12.45651
125d                  11.74433 0.1663130  6 11.53504 12.02412
14d                   11.54073 0.3877921  8 10.80300 11.96797
Ground                12.16673 0.3391952 12 11.56278 12.86199

$comparison
NULL

$groups
     trt    means  M
1 Ground 12.16673  a
2 111d   11.86369 ab
3 125d   11.74433 ab
4 14d    11.54073  b

我希望我的输出看起来像这样:

0001 a ab ab b
0002 ...
0003 ...
...

我怎么能在循环中得到这样的结果?

我收到了一个错误:

   for (i in 6:m){ 
Tukey1=HSD.test(lm(sdata[,i] ~ sdata$Medium+sdata$color+sdata$type+sdata$Micro), 'sdata$Micro') 
x[i,] = Tukey1$groups[1:4,3]
} 

Error in row.names<-.data.frame(*tmp*, value = value) : missing values in 'row.names' are not allowed

Medium  color   type    Micro   replication JAT_0001    JAT_0002    JAT_UF_0003   .......
T13 Br  ST  14d 1   7.796561869 10.25722947 8.358342094
T13 Br  ST  111d    1   7.725102551 10.49954075 8.926736251
T13 Br  ST  125d    1   7.76897864  10.60934327 9.593081824
T13 Br  ST  125d    2   7.727733885 10.43269524 9.157324235
T13 Br  CO  14d 1   7.744205976 10.20154774 8.610439104
T13 Br  CO  111d    1   7.668092713 10.19312878 8.845051329
T13 Br  CO  125d    1   7.841236441 10.21631771 8.199416713
T13 Br  TL  Ground  1   7.437145528 10.6563327  8.957033378
T13 Br  TL  14d 1   7.609625475 10.49023043 8.896758964
T13 In  ST  Ground  1   7.595451012 10.80042474 9.464399064
T13 In  ST  Ground  2   7.730454076 10.64082958 8.542183261
T13 In  ST  111d    1   8.219528235 10.16869956 8.751080927
T13 In  TL  Ground  1   7.622781002 10.78092932 9.340316315
T2  Br  ST  14d 1   7.659787195 10.13839983 8.175650644
T2  Br  ST  14d 2   8.622211514 10.04158218 6.838194468
T2  Br  ST  14d 3   8.890290175 9.588902037 7.879420933
T2  Br  ST  Ground  1   7.961193023 10.16522895 8.81688728
T2  Br  CO  Ground  1   7.778931896 10.69110829 8.941482896
T2  Br  CO  Ground  2   8.038375873 10.57522016 8.982078909
T2  Br  CO  Ground  3   7.953854738 10.12257326 8.471493439
T2  Br  CO  111d    1   7.661298122 10.35416158 8.628662747
T2  Br  TL  Ground  1   7.766862289 10.92627748 9.9706205
T2  Br  TL  111d    1   7.899306069 9.796455434 7.92545749
T2  Br  TL  111d    2   8.062080142 9.812688772 8.186133545
T2  In  CO  Ground  1   7.717997141 10.0607044  8.413483731
T2  In  CO  14d 1   8.589243939 9.844666572 9.174649637
T2  In  CO  14d 2   8.207486485 10.78201791 9.450837609

1 个答案:

答案 0 :(得分:0)

这是您的实际代码吗?如果是这样,您的for()声明就会被抬高。尝试

for (i in 1:m){ 
  Tukey1 = HSD.test(lm(sdata[,i] ~ sdata$Medium+sdata$color+sdata$type+sdata$Micro), 'sdata$Micro')
  x[i,] = Tukey1$goups[1:4,3]
}

您还没有描述您遇到的错误,但似乎您的问题只是提取您想要的部分结果,而不是运行回归和测试。如果这是真的,如果上述代码无法解决您的问题,请告诉我们出现的错误/警告并提供Tukey1作为数据,而不是您的原始数据(我不认为原始数据)数据有帮助)。

表面上看起来除了我指出的问题外,你的代码还可以,所以你遇到的错误是一些小细节。这就是为什么人们想要可复制的代码,而不仅仅是模糊的“它不起作用。”

您可能还会更改问题中的某些措辞,因为您的问题中没有其他内容表明您很难将任何内容保存到文件中。看起来你很难将结果复制到矩阵中。