Re: [R] What is wrong with this FOR-loop?

From: Barry Rowlingson <B.Rowlingson_at_lancaster.ac.uk>
Date: Mon 05 Dec 2005 - 22:56:06 EST

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+j

  have 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

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



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 Received on Mon Dec 05 23:06:51 2005

This archive was generated by hypermail 2.1.8 : Tue 06 Dec 2005 - 02:26:29 EST