Benilton Carvalho
2007-May-17 14:56 UTC
[R] help with executing instruction every i-th run of loop
if (i %% 1000 == 0) b On May 17, 2007, at 10:56 AM, Mark W Kimpel wrote:> I am running a very long loop and would like to save intermediate > results in case of a system or program crash. Here is the skeleton of > what my code would be: > > for (i in 1:zillion) > { > results[[i]]<-do.something.function() > if (logical.test(i)) {save(results, "results.tmp")} > } > > logical.test would test to see if i/1000 has no remainder. What R > function would test that? > > Is there an even better way to address my need? > > Thanks, > Mark > -- > > --- > > Mark W. Kimpel MD ** Neuroinformatics ** Dept. of Psychiatry > Indiana University School of Medicine > > 15032 Hunter Court, Westfield, IN 46074 > > (317) 490-5129 Work, & Mobile & VoiceMail > (317) 663-0513 Home (no voice mail please) > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code.-- Benilton Carvalho PhD Candidate Department of Biostatistics Bloomberg School of Public Health Johns Hopkins University bcarvalh at jhsph.edu
Mark W Kimpel
2007-May-17 14:56 UTC
[R] help with executing instruction every i-th run of loop
I am running a very long loop and would like to save intermediate results in case of a system or program crash. Here is the skeleton of what my code would be: for (i in 1:zillion) { results[[i]]<-do.something.function() if (logical.test(i)) {save(results, "results.tmp")} } logical.test would test to see if i/1000 has no remainder. What R function would test that? Is there an even better way to address my need? Thanks, Mark -- --- Mark W. Kimpel MD ** Neuroinformatics ** Dept. of Psychiatry Indiana University School of Medicine 15032 Hunter Court, Westfield, IN 46074 (317) 490-5129 Work, & Mobile & VoiceMail (317) 663-0513 Home (no voice mail please)
Uwe Ligges
2007-May-17 14:59 UTC
[R] help with executing instruction every i-th run of loop
Mark W Kimpel wrote:> I am running a very long loop and would like to save intermediate > results in case of a system or program crash. Here is the skeleton of > what my code would be: > > for (i in 1:zillion) > { > results[[i]]<-do.something.function() > if (logical.test(i)) {save(results, "results.tmp")} > } > > logical.test would test to see if i/1000 has no remainder. What R > function would test that?!(i %% 1000) Uwe Ligges> Is there an even better way to address my need? > > Thanks, > Mark
Duncan Murdoch
2007-May-17 14:59 UTC
[R] help with executing instruction every i-th run of loop
On 5/17/2007 10:56 AM, Mark W Kimpel wrote:> I am running a very long loop and would like to save intermediate > results in case of a system or program crash. Here is the skeleton of > what my code would be: > > for (i in 1:zillion) > { > results[[i]]<-do.something.function() > if (logical.test(i)) {save(results, "results.tmp")} > } > > logical.test would test to see if i/1000 has no remainder. What R > function would test that?(i %% 1000) == 0 Duncan Murdoch
Barry Rowlingson
2007-May-17 15:17 UTC
[R] help with executing instruction every i-th run of loop
Mark W Kimpel wrote:> I am running a very long loop and would like to save intermediate > results in case of a system or program crash. Here is the skeleton of > what my code would be: > > for (i in 1:zillion)I'm a bit worried about this line: > 1:zillion > Error: cannot allocate vector of size 4 zillion bytes hmm, lets try on a machine with a few more zillion bytes of RAM: > 1:zillion Error: result would be too long a vector> Is there an even better way to address my need?Looping over vectors like this involves the uneccesary creation of a long vector. For anything up to a million its probably okay, but once you start getting into the zillions... You can do it with less storage by just having a while loop: > while (i != 100 ){print(i);i=i+1} Many modern computer languages have "iterators" for looping, which abstract all the looping functionality into an object. I started writing something for R a few years ago but never got round to finishing it. It let you do this: myLoop <- loop(N=10,step=1,start=1) while(iterate(myLoop)){ doSomething() } The 'myLoop' object here is the iterator that controls the looping. You can use it to get the iteration number and then use the i %% 1000 test everyone else has told you about by now... Anyway, if anyone has a spare R programmer kicking around and would like all my looper code, just ask... Barry