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

Mark Knecht markknecht at gmail.com
Sun Jul 12 20:53:18 CEST 2009


Hi,
   Newbie alert on for loops...

   I have a bunch of data.frames built using rbind that have repeated
values in the EnTime column. I want to read the value in the EnTime
column and use it as an input to a function, but only down to the
first occurrence of the string "(all)" where I want to break off and
do other things. From the results I'm getting from some test code I
clearly don't understand how this is supposed to work.

   Here's a picture of what's in the first column of a sample data.frame:

> SystemResults$EnTime
 [1] 853   906   919   932   945   958   1011  1024  1037  1050  1103
1116  1129  1142  1155  1208  1221  1234  1247  1300  (all) 853   906
 919   932   945   958   1011  1024  1037  1050
[32] 1103  1116  1129  1142  1155  1208  1221  1234  1247  1300  (all)
Levels: 853 906 919 932 945 958 1011 1024 1037 1050 1103 1116 1129
1142 1155 1208 1221 1234 1247 1300 (all)
> class(SystemResults$EnTime)
[1] "factor"
> mode(SystemResults$EnTime)
[1] "numeric"
>

   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)
	X = SystemResults$EnTime[n]
	print(X)
}

   What I hoped to see was
853
906
919
932
945
...
1234
1247
1300

   My presumption was that I would eventually use X as the input to
another function that does the work I want to do but this prints so
much garbage that I'm clearly in the dark.

   Thanks in advance to anyone able to straighten me out.

Cheers,
Mark




More information about the R-help mailing list