i'm new to R. I have a question about how to save results in a loop to a file. Here is an example:
# 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]
}
I don't know how to do it with the loop for all data, so i just tried one of them,
Tukey1 = HSD.test(lm(sdata[,1] ~ sdata$Medium+sdata$color+sdata$type+sdata$Micro), 'sdata$Micro')
Tukey1
The results look like this:
$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
I want my output look like this:
0001 a ab ab b
0002 ...
0003 ...
...
How could i get such results in the loop?
I got an error:
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
Is this your actual code? If so, your for()
statement is janked up. Try
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]
}
You haven't described what errors you are getting but it seems like your issue is just extracting the part of the results you want, not running the regressions and tests. If that is true, and if the above code doesn't fix your issue, please let us know what errors/warnings come up and provide Tukey1
as data, rather than your raw data (I don't think the raw data helps).
Superficially it looks like aside from the issue I pointed out, your code is ok, so the bug you are experiencing is some small detail. That's why people want reproduceable code rather than just a vague "it isn't working."
You might also change some of the wording in your question as nothing else in your question suggests you are having a hard time saving anything to a file. It seems like you are just have a hard time copying results into a matrix.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.