Hi, I have a more complex example, but the problem boils down to this FOR-loop not filling in the res-matrix run_rows<-seq(0,1,0.05) run_cols<-seq(0.3,0.6,0.05) res<-matrix(NA,length(run_rows),length(run_cols)) for(i in run_rows) { for(j in run_cols) { res[i,j]=i+j #niether the above, nor res[[i,j]]=i+j work, why? } } Thank you, Serguei
Hi, I guess that res[i,j] needs integers as indexes. I would try it like this: (corrected)> run_rows<-seq(0,1,0.05) > run_cols<-seq(0.3,0.6,0.05) > > res<-matrix(NA,length(run_rows),length(run_cols)) > > for(i in 1:length(run_rows)) > { > for(j in 1:length(run_cols)) > { > res[i,j]=run_rows[i]+run_cols[j] > } > } > > Thank you, > SergueiChristian> > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >*** --- *** Christian Hennig University College London, Department of Statistical Science Gower St., London WC1E 6BT, phone +44 207 679 1698 chrish at stats.ucl.ac.uk, www.homepages.ucl.ac.uk/~ucakche
Serguei Kaniovski wrote:> Hi, I have a more complex example, but the problem boils down to this > FOR-loop not filling in the res-matrix> for(i in run_rows) > { > for(j in run_cols) > { > res[i,j]=i+jhave a look at what i and j are in such a loop: > for(i in run_rows){ + print(i) + } [1] 0 [1] 0.05 [1] 0.1 [1] 0.15 [1] 0.2 [1] 0.25 - and then you are trying to fill res[0.05,.3] and so on. Its not going to work! You probably want something like: for(i in 1:length(run_rows)){ so that i is 1,2,3,.... then you do: res[i,j] = run_rows[i] + run_cols[j] within your loop... ...which in fact can be done in one line, but you need to learn about matrix indexing first! Barry
try this: (you were trying to index with non-integer numbers) run_rows<-seq(0,1,0.05) run_cols<-seq(0.3,0.6,0.05) res<-matrix(NA,length(run_rows),length(run_cols)) for(i in 1:length(run_rows)) { for(j in 1:length(run_cols)) { res[i,j] = run_rows[i] + run_cols[j] #niether the above, nor res[[i,j]]=i+j work, why? } } On 12/5/05, Serguei Kaniovski <kaniovsk@wifo.ac.at> wrote:> > Hi, I have a more complex example, but the problem boils down to this > FOR-loop not filling in the res-matrix > > run_rows<-seq(0,1,0.05) > run_cols<-seq(0.3,0.6,0.05) > > res<-matrix(NA,length(run_rows),length(run_cols)) > > for(i in run_rows) > { > for(j in run_cols) > { > res[i,j]=i+j > #niether the above, nor res[[i,j]]=i+j work, why? > } > } > > Thank you, > Serguei > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 247 0281 What the problem you are trying to solve? [[alternative HTML version deleted]]
Christian Hennig wrote:>>run_rows<-seq(0,1,0.05) >>run_cols<-seq(0.3,0.6,0.05) >> >>res<-matrix(NA,length(run_rows),length(run_cols)) >> >>for(i in 1:length(run_rows)) >>{ >> for(j in 1:length(run_cols)) >> { >> res[i,j]=run_rows[i]+run_cols[j] >> } >>}Or the one-liner: res = outer(run_rows,run_cols,"+") but maybe its good to learn about array indexing first... Barry
your indexing is wrong; another way to achieve the same result is outer(run_rows, run_cols, "+") I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Serguei Kaniovski" <kaniovsk at wifo.ac.at> To: <r-help at stat.math.ethz.ch> Sent: Monday, December 05, 2005 12:47 PM Subject: [R] What is wrong with this FOR-loop?> Hi, I have a more complex example, but the problem boils down to > this > FOR-loop not filling in the res-matrix > > run_rows<-seq(0,1,0.05) > run_cols<-seq(0.3,0.6,0.05) > > res<-matrix(NA,length(run_rows),length(run_cols)) > > for(i in run_rows) > { > for(j in run_cols) > { > res[i,j]=i+j > #niether the above, nor res[[i,j]]=i+j work, why? > } > } > > Thank you, > Serguei > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm