poastpd at umich.edu
2008-Sep-12 13:36 UTC
[R] Loop to fill in array skips over certain columns
Hello, Below is a simple loop that should place a 3 in the first row of each column in the array "List". iterations<-3 tweaksize<-0.00 ii <-1 List=array(-1000, dim=c(iterations,11)) colnames(List) <- c("orig", "0.05", "0.10", "0.15","0.20","0.25","0.30","0.35","0.40","0.45","0.50") Entry=3 while (tweaksize<=0.5) { if (tweaksize==0.00) coll = 1 if (tweaksize==0.05) coll = 2 if (tweaksize==0.10) coll = 3 if (tweaksize==0.15) coll = 4 if (tweaksize==0.20) coll = 5 if (tweaksize==0.25) coll = 6 if (tweaksize==0.30) coll = 7 if (tweaksize==0.35) coll = 8 if (tweaksize==0.40) coll = 9 if (tweaksize==0.45) coll = 10 if (tweaksize==0.50) coll = 11 List[ii,coll]=Entry tweaksize = tweaksize + 0.05 } List However, if I run the loop, R skips the 4th column (0.15), 9th column (0.40), 10th column (0.45), and eleventh column (0.50). Below is the output: orig 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 [1,] 3 3 3 -1000 3 3 3 3 -1000 -1000 -1000 [2,] -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 [3,] -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 Can anyone please suggest a reason why R is skipping these four columns? Any help will be greatly appreciated. Thank you, Paul
FAQ 7.31 -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of poastpd at umich.edu > Sent: Friday, September 12, 2008 7:36 AM > To: r-help at r-project.org > Subject: [R] Loop to fill in array skips over certain columns > > Hello, > > Below is a simple loop that should place a 3 in the first row > of each column in the array "List". > > iterations<-3 > tweaksize<-0.00 > ii <-1 > List=array(-1000, dim=c(iterations,11)) > colnames(List) <- c("orig", "0.05", "0.10", > "0.15","0.20","0.25","0.30","0.35","0.40","0.45","0.50") > Entry=3 > > while (tweaksize<=0.5) { > if (tweaksize==0.00) coll = 1 > if (tweaksize==0.05) coll = 2 > if (tweaksize==0.10) coll = 3 > if (tweaksize==0.15) coll = 4 > if (tweaksize==0.20) coll = 5 > if (tweaksize==0.25) coll = 6 > if (tweaksize==0.30) coll = 7 > if (tweaksize==0.35) coll = 8 > if (tweaksize==0.40) coll = 9 > if (tweaksize==0.45) coll = 10 > if (tweaksize==0.50) coll = 11 > > List[ii,coll]=Entry > tweaksize = tweaksize + 0.05 > } > > List > > However, if I run the loop, R skips the 4th column (0.15), > 9th column (0.40), 10th column (0.45), and eleventh column > (0.50). Below is the > output: > > orig 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 > 0.45 0.50 > [1,] 3 3 3 -1000 3 3 3 3 -1000 -1000 -1000 > [2,] -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 > -1000 -1000 [3,] -1000 -1000 -1000 -1000 -1000 -1000 -1000 > -1000 -1000 -1000 -1000 > > Can anyone please suggest a reason why R is skipping these > four columns? Any help will be greatly appreciated. > > Thank you, > > Paul > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >