[R] nested 'while' loops

Patrick Burns pburns at pburns.seanet.com
Mon Mar 25 19:18:40 CET 2013


Your immediate problem is that 'y' is
not reset to 1.

Easier code to write would be to use
'for' loops rather than 'while' loops.

Better still would be to use neither if
possible.  I suspect that you are in
Circle 3 of 'The R Inferno'.

http://www.burns-stat.com/documents/books/the-r-inferno/

Pat


On 25/03/2013 17:43, Sahana Srinivasan wrote:
> Hi everyone,
> I'm using the following code to go over every element of a data frame (row
> wise). The problem I am facing is that the outer 'x' variable is not
> incrementing itself, thus, only one row of values is obtained, and the
> program does not proceed to the next row.
>
> This is the code:
>   while(x<=coln)
>   {
>
>     while(y<=rown)
>     {
>       n<-as.numeric(df[[y]][x]);
>        if(n>0)
>        {
>          lim<-(n-1);
>          S<-100;
>         (...)
>          }
>          opdf[[y]][x]<-sum;
>        }
>        y<-y+1;
> }
>    x<-x+1;
> }
>
> Here is a sample of the input data:
>
>
> GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 4229 39635
> 20 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 914 2120 30
> 0 8
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

-- 
Patrick Burns
pburns at pburns.seanet.com
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of:
  'Impatient R'
  'The R Inferno'
  'Tao Te Programming')



More information about the R-help mailing list