[R] for (n in SystemResults$EnTime) return EnTime[n] until reaching "(all)"

Petr PIKAL petr.pikal at precheza.cz
Mon Jul 13 09:32:24 CEST 2009


Hi

r-help-bounces at r-project.org napsal dne 12.07.2009 22:24:29:

> On Sun, Jul 12, 2009 at 1:05 PM, David Winsemius<dwinsemius at comcast.net> 
wrote:
> >
> > On Jul 12, 2009, at 3:35 PM, David Winsemius wrote:
> >
> >>
> >> On Jul 12, 2009, at 2:53 PM, Mark Knecht wrote:
> >>
> >>>
> >>>  As a test I tried to print down to the string "(all)" and then
> >>> break but this code and everything I've tried so far is terribly
> >>> wrong. Every attempt prints lots of error messages. I'm not grasping
> >>> at all what I'm doing wrong or what's the right way to do this sort 
of
> >>> thing. Clearly my first for loop isn't a success!
> >>>
> >>> for(n in SystemResults$EnTime)  {
> >>>        if(SystemResults$EnTime[n] == "(all)") break)
> >>
> >> Inside the loop, shouldn't you be comparing to "n"?? As you have it 
now,
> >> the values of that factor are probably being used as indices to 
itself. (Not
> >> good.) Also not good is the use of "break".  It looks to be fairly 
severely
> >> deprecated at this point
> >
> > Appears I am wrong about this. I was basing my assumption on this
> > interaction with the R interpreter:
> >> ?break
> > Error in genericForPrimitive(f) :
> >  methods may not be defined for primitive function "break" in this 
version
> > of R
> >
> > But:
> >
> > ?Control ... suggests that break-ing out of for loops remains 
acceptable.
> >
> >
> > David Winsemius, MD
> > Heritage Laboratories
> > West Hartford, CT
> >
> >
> 
> Hi David,
>    Thanks for the response. It is helping.
> 
>    I think the break is required as your suggestion doesn't exit the
> loop i there is more data like mine. It just skips printing the (all)
> but incorrectly prints the other copies down lower in the data frame:
> 
> >  tf <- factor(c(53 ,  906  , 919  , 932 ,  945 ,  958 ,  1011 , 1024 , 

> "(all)", 53 ,  906  , 919  , 932 ,  945 ,  958 ,  1011 , 1024 , "(all)" 
) )
> >  for(n in tf ) {if (n != "(all)")  print(n)}
> [1] "53"
> [1] "906"
> [1] "919"
> [1] "932"
> [1] "945"
> [1] "958"
> [1] "1011"
> [1] "1024"
> [1] "53"
> [1] "906"
> [1] "919"
> [1] "932"
> [1] "945"
> [1] "958"
> [1] "1011"
> [1] "1024"
> >
> 
> whereas the else break gets me out:
> 
> >  tf <- factor(c(53 ,  906  , 919  , 932 ,  945 ,  958 ,  1011 , 1024 , 

> "(all)", 53 ,  906  , 919  , 932 ,  945 ,  958 ,  1011 , 1024 , "(all)" 
) )
> >  for(n in tf ) {if (n != "(all)")  print(n) else break}
> [1] "53"
> [1] "906"
> [1] "919"
> [1] "932"
> [1] "945"
> [1] "958"
> [1] "1011"
> [1] "1024"
> >
> 
> My confusion here is really how the 'n' is being used. I thought it
> was just an index - a number that gets used inside of the curly braces
> like other languages I've used. It seems it isn't that at all but
> really operates as something that returns the actual value of the
> position in the factor. I was trying to reference the location in the
> list but for is already returning the value. Strange, but I'm sure
> there are good reasons. Please note that I anot a prgrammer and have
> no formal training so it hardly matters what I think! :-)
> 
> I'm now wondering if I'd be better off to try using ?match to find the
> first position of "(all)" instead of using the for loop? If match

like that?

min(which(tf %in% "(all)"))

Regards
Petr


> returned a number then I think I'd be more comfortable, but maybe I
> should keep going the way I am.
> 
> It seems I'm maybe getting the getting the correct answer now but I'm
> concerned that it's coming back with quotes. I can get around that
> using
> 
> print(as.integer(x))
> 
> but all this coercion stuff that R is doing is giving me fits. I just
> don't have my head around it yet. None the less R is already giving me
> visibility into my data that I've not had before so overall the
> results are strongly positive.
> 
> Thanks,
> Mark
> 
> ______________________________________________
> 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.




More information about the R-help mailing list