Loop overwriting

孤街浪徒 提交于 2019-12-25 06:46:29

问题


I have R version 3.1.0 and am using Windows 7. I am trying to write a loop for a dataset with 474 rows.
For every unique ID value I can have several values for the tow column. The strata and number columns are the same for all unique values of ID.

I would like to calculate the variance with this equation:

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

for all unique ID values. From what I have been reading online it looks like my loop is overwriting itself. To address this I tried adding in the last four lines of code, but I am still getting an error message: replacement has 10 rows, data has 1.

Here is my data:

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")

My attempt at a loop

#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:


what you need is a table with the name of each element and with an identifier (column names: id & name, in this order; it could be in your input table with the rest of your data). After that you can write this:

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

the model will run from the last to the first element, because in the next line is defined that way.

#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}   }

this part is very important, you need to have one file with the structure you want, i mean the column names at least.

#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)}}
 }


来源:https://stackoverflow.com/questions/24703724/loop-overwriting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!