Re: [R] controlling the number of times a script is repeated in a loop

2008-12-01 Thread Carl Witthoft

quote:
On Sun, Nov 30, 2008 at 12:21 PM, Salas, Andria Kay 
aks2515_at_uncw.edu wrote:
 I am running a large for loop and at the end of each iteration a 
matrix is produced. The program changes the columns in the matrix, and 
each time a column is added the name of that column is y. All original 
columns have no column name. Due to the nature of the program, all 
original columns will eventually be replaced with new columns each with 
the column name y. It is at this point that I want the program to stop 
running the for loop (right now I just have it running for 20 iterations 
resulting in a matrix with a mix of old and new columns). I was thinking 
maybe I could do something with an if else statement and the command 
colnames. For example:


 If(colnames all are y) (stop) else (run the program again)

Have a look at ?while :

while(!(colnames all are y)) { run the program again }

Hadley
endquote

I may be misunderstanding you, but if you know the number of columns in 
the matrix, and on every iteration, one of them is changed, why not just 
set the loop up as


Rfor (i in 1:ncol(your_matrix))?


Carl

__
R-help@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.


[R] controlling the number of times a script is repeated in a loop

2008-11-30 Thread Salas, Andria Kay
I am running a large for loop and at the end of each iteration a matrix is 
produced.  The program changes the columns in the matrix, and each time a 
column is added the name of that column is y.  All original columns have no 
column name.  Due to the nature of the program, all original columns will 
eventually be replaced with new columns each with the column name y. It is at 
this point that I want the program to stop running the for loop (right now I 
just have it running for 20 iterations resulting in a matrix with a mix of old 
and new columns).  I was thinking maybe I could do something with an if else 
statement and the command colnames.  For example:

If(colnames all are y) (stop) else (run the program again)

Not entirely sure how to go about this though, or if I am even on the right 
track.  Maybe I need to get rid of the master for loop as perhaps for loops are 
more constraining in the number of times the program is run?  The number of 
runs to get all original columns replaced with new ones will never be the same 
(I plan on running this several times, with time steps ending when all original 
columns are replaced.  Perhaps at the end of each time step I can set column 
names to have no column names (so from ys to nothing), allowing me to use 
this method for each time step)...

I know it is always helpful to have some code to work with, but it seems 
impractical to send the whole program as one part if it I am still working on 
and would probably just create more confusion.  Hopefully someone might be able 
to offer a few suggestions...I can try to come up with a toy program to send 
along but I am not good enough in R yet to do this quickly!

Thank you for all the help and I am sorry if this question is perhaps a bit 
confusing!  If I have not made my problem and what I hope to do clear, please 
let me know.

__
R-help@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.


Re: [R] controlling the number of times a script is repeated in a loop

2008-11-30 Thread hadley wickham
On Sun, Nov 30, 2008 at 12:21 PM, Salas, Andria Kay [EMAIL PROTECTED] wrote:
 I am running a large for loop and at the end of each iteration a matrix is 
 produced.  The program changes the columns in the matrix, and each time a 
 column is added the name of that column is y.  All original columns have no 
 column name.  Due to the nature of the program, all original columns will 
 eventually be replaced with new columns each with the column name y. It is 
 at this point that I want the program to stop running the for loop (right now 
 I just have it running for 20 iterations resulting in a matrix with a mix of 
 old and new columns).  I was thinking maybe I could do something with an if 
 else statement and the command colnames.  For example:

 If(colnames all are y) (stop) else (run the program again)

Have a look at ?while :

while(!(colnames all are y)) { run the program again }

Hadley

-- 
http://had.co.nz/

__
R-help@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.