循环覆盖

时间:2014-07-11 17:56:04

标签: r loops overwrite

我有R版本3.1.0并且正在使用Windows 7.我正在尝试为474行的数据集编写一个循环。
对于每个唯一的ID值,我可以为拖链列提供多个值。对于ID的所有唯一值,分层和数字列是相同的。

我想用这个等式计算方差:

(sum((sall$tow-sall$strata)^2))/unique(sall$number)* (unique(sall$number)-1) 

表示所有唯一ID值。从我在线阅读看起来我的循环就是覆盖自己。为了解决这个问题,我尝试添加最后四行代码,但我仍然收到一条错误消息:replacement有10行,数据有1行。

这是我的数据:

dput(sall) 

structure(list(ID = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 5L, 5L, 5L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 9L, 9L, 9L, 10L, 
10L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 
13L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 
16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 23L, 
23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L), .Label = c("2007 Fall 0102", 
"2007 Fall 0201", "2007 Fall 0202", "2007 Fall 0301", "2007 Fall 0302", 
"2007 Fall 0401", "2007 Fall 0402", "2007 Fall 0501", "2007 Fall 0502", 
"2007 Fall 0601", "2007 Fall 0602", "2007 Fall 0701", "2007 Fall 0702", 
"2007 Fall 0801", "2007 Fall 0802", "2007 Fall 0901", "2007 Fall 0902", 
"2007 Fall 1001", "2007 Fall 1002", "2007 Fall 1101", "2007 Fall 1102", 
"2007 Fall 1201", "2007 Fall 1202", "2007 Fall 1301", "2007 Fall 1302", 
"2007 Fall 1401", "2007 Fall 1402", "2007 Fall 1501", "2007 Fall 1502", 
"2007 Fall BI03", "2007 Fall BI04", "2007 Fall RI03", "2007 Fall RI04", 
"2008 Fall 0102", "2008 Fall 0201", "2008 Fall 0202", "2008 Fall 0301", 
"2008 Fall 0302", "2008 Fall 0401", "2008 Fall 0402", "2008 Fall 0501", 
"2008 Fall 0502", "2008 Fall 0601", "2008 Fall 0602", "2008 Fall 0701", 
"2008 Fall 0702", "2008 Fall 0801", "2008 Fall 0802", "2008 Fall 0901", 
"2008 Fall 0902", "2008 Fall 0903", "2008 Fall 1001", "2008 Fall 1002", 
"2008 Fall 1101", "2008 Fall 1102", "2008 Fall 1201", "2008 Fall 1202", 
"2008 Fall 1301", "2008 Fall 1302", "2008 Fall 1401", "2008 Fall 1402", 
"2008 Fall 1501", "2008 Fall 1502", "2008 Fall BI03", "2008 Fall BI04", 
"2008 Fall RI03", "2008 Fall RI04", "2008 Spring 0102", "2008 Spring 0201", 
"2008 Spring 0202", "2008 Spring 0301", "2008 Spring 0302", "2008 Spring 0401", 
"2008 Spring 0402", "2008 Spring 0501", "2008 Spring 0502", "2008 Spring 0601", 
"2008 Spring 0602", "2008 Spring 0701", "2008 Spring 0702", "2008 Spring 0801", 
"2008 Spring 0802", "2008 Spring 0901", "2008 Spring 0902", "2008 Spring 1001", 
"2008 Spring 1002", "2008 Spring 1101", "2008 Spring 1102", "2008 Spring 1201", 
"2008 Spring 1202", "2008 Spring 1301", "2008 Spring 1302", "2008 Spring 1401", 
"2008 Spring 1402", "2008 Spring 1501", "2008 Spring 1502", "2008 Spring BI03", 
"2008 Spring BI04", "2008 Spring RI03", "2008 Spring RI04", "2009 Fall 0102", 
"2009 Fall 0201", "2009 Fall 0202", "2009 Fall 0301", "2009 Fall 0302", 
"2009 Fall 0401", "2009 Fall 0402", "2009 Fall 0501", "2009 Fall 0502", 
"2009 Fall 0601", "2009 Fall 0602", "2009 Fall 0701", "2009 Fall 0702", 
"2009 Fall 0801", "2009 Fall 0802", "2009 Fall 0901", "2009 Fall 0902", 
"2009 Fall 0903", "2009 Fall 1001", "2009 Fall 1002", "2009 Fall 1101", 
"2009 Fall 1102", "2009 Fall 1201", "2009 Fall 1202", "2009 Fall 1301", 
"2009 Fall 1302", "2009 Fall 1401", "2009 Fall 1402", "2009 Fall 1501", 
"2009 Fall 1502", "2009 Fall BI03", "2009 Fall BI04", "2009 Fall RI03", 
"2009 Fall RI04", "2009 Spring 0102", "2009 Spring 0201", "2009 Spring 0202", 
"2009 Spring 0301", "2009 Spring 0302", "2009 Spring 0401", "2009 Spring 0402", 
"2009 Spring 0501", "2009 Spring 0502", "2009 Spring 0601", "2009 Spring 0602", 
"2009 Spring 0701", "2009 Spring 0702", "2009 Spring 0801", "2009 Spring 0802", 
"2009 Spring 0901", "2009 Spring 0902", "2009 Spring 0903", "2009 Spring 1001", 
"2009 Spring 1002", "2009 Spring 1101", "2009 Spring 1102", "2009 Spring 1201", 
"2009 Spring 1202", "2009 Spring 1301", "2009 Spring 1302", "2009 Spring 1401", 
"2009 Spring 1402", "2009 Spring 1501", "2009 Spring 1502", "2009 Spring BI03", 
"2009 Spring BI04", "2009 Spring RI03", "2009 Spring RI04", "2010 Fall 0102", 
"2010 Fall 0201", "2010 Fall 0202", "2010 Fall 0301", "2010 Fall 0302", 
"2010 Fall 0401", "2010 Fall 0402", "2010 Fall 0501", "2010 Fall 0502", 
"2010 Fall 0601", "2010 Fall 0602", "2010 Fall 0701", "2010 Fall 0702", 
"2010 Fall 0801", "2010 Fall 0802", "2010 Fall 0901", "2010 Fall 0902", 
"2010 Fall 0903", "2010 Fall 1001", "2010 Fall 1002", "2010 Fall 1101", 
"2010 Fall 1102", "2010 Fall 1201", "2010 Fall 1202", "2010 Fall 1301", 
"2010 Fall 1302", "2010 Fall 1401", "2010 Fall 1402", "2010 Fall 1501", 
"2010 Fall 1502", "2010 Fall BI03", "2010 Fall BI04", "2010 Fall RI03", 
"2010 Fall RI04", "2010 Spring 0102", "2010 Spring 0201", "2010 Spring 0202", 
"2010 Spring 0301", "2010 Spring 0302", "2010 Spring 0401", "2010 Spring 0402", 
"2010 Spring 0501", "2010 Spring 0502", "2010 Spring 0601", "2010 Spring 0602", 
"2010 Spring 0701", "2010 Spring 0702", "2010 Spring 0801", "2010 Spring 0802", 
"2010 Spring 0901", "2010 Spring 0902", "2010 Spring 0903", "2010 Spring 1001", 
"2010 Spring 1002", "2010 Spring 1101", "2010 Spring 1102", "2010 Spring 1201", 
"2010 Spring 1202", "2010 Spring 1301", "2010 Spring 1302", "2010 Spring 1401", 
"2010 Spring 1402", "2010 Spring 1501", "2010 Spring 1502", "2010 Spring BI03", 
"2010 Spring BI04", "2010 Spring RI03", "2010 Spring RI04", "2011 Fall 0102", 
"2011 Fall 0201", "2011 Fall 0202", "2011 Fall 0301", "2011 Fall 0302", 
"2011 Fall 0401", "2011 Fall 0402", "2011 Fall 0501", "2011 Fall 0502", 
"2011 Fall 0601", "2011 Fall 0602", "2011 Fall 0701", "2011 Fall 0702", 
"2011 Fall 0801", "2011 Fall 0802", "2011 Fall 0901", "2011 Fall 0902", 
"2011 Fall 0903", "2011 Fall 1001", "2011 Fall 1002", "2011 Fall 1101", 
"2011 Fall 1102", "2011 Fall 1201", "2011 Fall 1202", "2011 Fall 1301", 
"2011 Fall 1302", "2011 Fall 1401", "2011 Fall 1402", "2011 Fall 1501", 
"2011 Fall 1502", "2011 Fall BI03", "2011 Fall BI04", "2011 Fall RI03", 
"2011 Fall RI04", "2011 Spring 0102", "2011 Spring 0201", "2011 Spring 0202", 
"2011 Spring 0301", "2011 Spring 0302", "2011 Spring 0401", "2011 Spring 0402", 
"2011 Spring 0501", "2011 Spring 0502", "2011 Spring 0601", "2011 Spring 0602", 
"2011 Spring 0701", "2011 Spring 0702", "2011 Spring 0801", "2011 Spring 0802", 
"2011 Spring 0901", "2011 Spring 0902", "2011 Spring 0903", "2011 Spring 1001", 
"2011 Spring 1002", "2011 Spring 1101", "2011 Spring 1102", "2011 Spring 1201", 
"2011 Spring 1202", "2011 Spring 1301", "2011 Spring 1302", "2011 Spring 1401", 
"2011 Spring 1402", "2011 Spring 1501", "2011 Spring 1502", "2011 Spring BI03", 
"2011 Spring BI04", "2011 Spring RI03", "2011 Spring RI04", "2012 Fall 0102", 
"2012 Fall 0201", "2012 Fall 0202", "2012 Fall 0301", "2012 Fall 0302", 
"2012 Fall 0401", "2012 Fall 0402", "2012 Fall 0501", "2012 Fall 0502", 
"2012 Fall 0601", "2012 Fall 0602", "2012 Fall 0701", "2012 Fall 0702", 
"2012 Fall 0801", "2012 Fall 0802", "2012 Fall 0901", "2012 Fall 0902", 
"2012 Fall 0903", "2012 Fall 1001", "2012 Fall 1002", "2012 Fall 1101", 
"2012 Fall 1102", "2012 Fall 1201", "2012 Fall 1202", "2012 Fall 1301", 
"2012 Fall 1302", "2012 Fall 1401", "2012 Fall 1402", "2012 Fall 1501", 
"2012 Fall 1502", "2012 Fall BI03", "2012 Fall BI04", "2012 Fall RI03", 
"2012 Fall RI04", "2012 Spring 0102", "2012 Spring 0201", "2012 Spring 0202", 
"2012 Spring 0301", "2012 Spring 0302", "2012 Spring 0401", "2012 Spring 0402", 
"2012 Spring 0501", "2012 Spring 0502", "2012 Spring 0601", "2012 Spring 0602", 
"2012 Spring 0701", "2012 Spring 0702", "2012 Spring 0801", "2012 Spring 0802", 
"2012 Spring 0901", "2012 Spring 0902", "2012 Spring 0903", "2012 Spring 1001", 
"2012 Spring 1002", "2012 Spring 1101", "2012 Spring 1102", "2012 Spring 1201", 
"2012 Spring 1202", "2012 Spring 1301", "2012 Spring 1302", "2012 Spring 1401", 
"2012 Spring 1402", "2012 Spring 1501", "2012 Spring 1502", "2012 Spring BI03", 
"2012 Spring BI04", "2012 Spring RI03", "2012 Spring RI04", "2013 Fall 0102", 
"2013 Fall 0201", "2013 Fall 0202", "2013 Fall 0301", "2013 Fall 0302", 
"2013 Fall 0401", "2013 Fall 0402", "2013 Fall 0501", "2013 Fall 0502", 
"2013 Fall 0601", "2013 Fall 0602", "2013 Fall 0701", "2013 Fall 0702", 
"2013 Fall 0801", "2013 Fall 0802", "2013 Fall 0901", "2013 Fall 0902", 
"2013 Fall 0903", "2013 Fall 1001", "2013 Fall 1002", "2013 Fall 1101", 
"2013 Fall 1102", "2013 Fall 1201", "2013 Fall 1202", "2013 Fall 1301", 
"2013 Fall 1302", "2013 Fall 1401", "2013 Fall 1402", "2013 Fall 1501", 
"2013 Fall 1502", "2013 Fall BI03", "2013 Fall BI04", "2013 Fall RI03", 
"2013 Fall RI04", "2013 Spring 0102", "2013 Spring 0201", "2013 Spring 0202", 
"2013 Spring 0301", "2013 Spring 0302", "2013 Spring 0401", "2013 Spring 0402", 
"2013 Spring 0501", "2013 Spring 0502", "2013 Spring 0601", "2013 Spring 0602", 
"2013 Spring 0701", "2013 Spring 0702", "2013 Spring 0801", "2013 Spring 0802", 
"2013 Spring 0901", "2013 Spring 0902", "2013 Spring 0903", "2013 Spring 1001", 
"2013 Spring 1002", "2013 Spring 1101", "2013 Spring 1102", "2013 Spring 1201", 
"2013 Spring 1202", "2013 Spring 1301", "2013 Spring 1302", "2013 Spring 1401", 
"2013 Spring 1402", "2013 Spring 1501", "2013 Spring 1502", "2013 Spring BI03", 
"2013 Spring BI04", "2013 Spring RI03", "2013 Spring RI04", "2014 Spring 0102", 
"2014 Spring 0201", "2014 Spring 0202", "2014 Spring 0301", "2014 Spring 0302", 
"2014 Spring 0401", "2014 Spring 0402", "2014 Spring 0501", "2014 Spring 0502", 
"2014 Spring 0601", "2014 Spring 0602", "2014 Spring 0701", "2014 Spring 0702", 
"2014 Spring 0801", "2014 Spring 0802", "2014 Spring 0901", "2014 Spring 0902", 
"2014 Spring 0903", "2014 Spring 1001", "2014 Spring 1002", "2014 Spring 1101", 
"2014 Spring 1102", "2014 Spring 1201", "2014 Spring 1202", "2014 Spring 1301", 
"2014 Spring 1302", "2014 Spring 1401", "2014 Spring 1402", "2014 Spring 1501", 
"2014 Spring 1502", "2014 Spring BI03", "2014 Spring BI04", "2014 Spring RI03", 
"2014 Spring RI04"), class = "factor"), tow = c(0, 0, 0, 0, 0, 
0, 2.23605155512019, 0, 0, 0, 0, 0, 9.82973042531156, 0, 13.2468201825844, 
0.891911996342454, 5.21675816713178, 0, 0, 15.2033670694891, 
1.89572410870025, 48.7412108012422, 0, 0, 1.95759259768715, 0, 
3.88468129963282, 0.929188282053128, 1.91044228649375, 0, 3.00339331972541, 
15.6004700096039, 1.89642204148978, 3.81625917124503, 4.93924505216363, 
0.928742305487474, 32.559107185651, 0, 0.967770730644331, 0, 
1.88722551009165, 1.06020636196734, 0.958346832196337, 37.9680437072546, 
0, 2.73697536715327, 3.95587835585049, 0, 2.45847705019412, 0, 
0, 0, 28.8384678112108, 0, 0, 0, 6.45504724311346, 4.23466581629433, 
0, 0, 8.87505991044715, 0, 0.977534888388763, 0, 0, 13.1789290003378, 
0, 0, 0, 0, 0, 1.0244278082888, 0, 0, 0, 0, 0, 0, 0, 2.7408696865954, 
0, 0, 0, 2.13057544602497, 0, 0, 0.974322578911629, 1.01665761052166, 
0, 0, 0, 0, 1.92641091919418, 1.8606348312538, 4.52036131549582, 
0, 1.02462932795132, 0, 0, 0), strata = c(0, 0, 0, 0, 0.745350518373397, 
0.745350518373397, 0.745350518373397, 0, 0, 0, 0, 0, 4.91486521265578, 
4.91486521265578, 6.45183011535287, 6.45183011535287, 6.45183011535287, 
0, 0, 21.9467673264772, 21.9467673264772, 21.9467673264772, 0, 
0, 1.94742463243999, 1.94742463243999, 1.94742463243999, 1.46075597206807, 
1.46075597206807, 1.46075597206807, 1.46075597206807, 9.9567076276068, 
9.9567076276068, 9.9567076276068, 9.9567076276068, 9.9567076276068, 
9.9567076276068, 0.483885365322166, 0.483885365322166, 6.3729711112376, 
6.3729711112376, 6.3729711112376, 6.3729711112376, 6.3729711112376, 
6.3729711112376, 6.3729711112376, 1.60358885151115, 1.60358885151115, 
1.60358885151115, 1.60358885151115, 4.94102260882732, 4.94102260882732, 
4.94102260882732, 4.94102260882732, 4.94102260882732, 4.94102260882732, 
4.94102260882732, 4.94102260882732, 0, 0, 2.87894047489672, 2.87894047489672, 
2.87894047489672, 2.87894047489672, 2.87894047489672, 2.87894047489672, 
2.87894047489672, 2.87894047489672, 0.204885561657759, 0.204885561657759, 
0.204885561657759, 0.204885561657759, 0.204885561657759, 0.304541076288377, 
0.304541076288377, 0.304541076288377, 0.304541076288377, 0.304541076288377, 
0.304541076288377, 0.304541076288377, 0.304541076288377, 0.304541076288377, 
0.62097960498732, 0.62097960498732, 0.62097960498732, 0.62097960498732, 
0.62097960498732, 0.490511421619306, 0.490511421619306, 0.490511421619306, 
0.490511421619306, 0.490511421619306, 0.490511421619306, 1.05794649638585, 
1.05794649638585, 1.05794649638585, 1.05794649638585, 1.05794649638585, 
1.05794649638585, 1.05794649638585), number = c(2, 2, 2, 2, 3, 
3, 3, 2, 2, 3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 3, 3, 
3, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 2, 2, 7, 7, 7, 7, 7, 7, 7, 4, 
4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 8, 8, 8, 8, 8, 8, 8, 8, 
5, 5, 5, 5, 5, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 5, 5, 5, 5, 6, 6, 
6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7)), .Names = c("ID", "tow", "strata", 
"number"), row.names = c(NA, 100L), class = "data.frame")

我尝试循环

#empty dataframe to hold loop output 
stratavar<-as.data.frame(matrix( 0, nrow=length(unique(one[,1])), ncol= 3 )) 

for(i in 1:length(unique(sall$ID))){ 

    new<-sall[sall$ID==i,] 
    stratavar[i,3]<-(sum((sall$tow-sall$strata)^2))/unique(sall$number)*
    (unique(sall$number)-1)   
    stratavar[i,1]<-as.character(unique(sall$ID)) 
    stratavar[i,2]<-unique(sall$strata) 

        if(i== "2007 Fall 0102" ) 
                newline<-data.frame(cbind(stratavar[i,1],stratavar[i,2],stratavar[i,3]))else{
                results<-data.frame(cbind(stratavar[i,1],stratavar[i,2],stratavar[i,3]))} 
                total<-rbind(newline,results) 
} 

1 个答案:

答案 0 :(得分:2)

你需要的是一个表格,其中包含每个元素的名称和一个标识符(列名:id&amp; name,按此顺序;它可以在您的输入表中包含其余数据)。之后你可以这样写:

#load the table with the name of the image & id 
TABLE<-read.table("Table.txt",header=T)

模型将从最后一个元素运行到第一个元素,因为在下一行中定义了这种方式。

#the range that your model will be running
i=max(TABLE$id)-1

#make the loop 
while ( i > 0)    {
i=i-1
#saves your function/algorithm in XXX
for(n in 1:length(TABLE[,1])) { if (TABLE[n,1]==i) {XXX=YOURFUNCTION}   }

这部分非常重要,你需要有一个你想要的结构文件,我的意思是至少列名。

#saves XXX in c:/CSV.csv
for(n in 1:length(TABLE[,1])) {if (TABLE[n,1]==i){write.table(XXX,"c:/CSV.csv",sep=";", append=TRUE, col.names=FALSE)}}
 }