[R] looping over factors

Samuelson, Frank* FWS4 at CDRH.FDA.GOV
Fri Jan 30 20:08:26 CET 2004


How does one loop over factors?  Perhaps this is a newbie question.
I tried:

> b
[1] caseX caseY caseZ
Levels: caseX caseY caseZ
> length(b)
[1] 3
>
> for (i in b) {
+   print (b == i) ;
+   print (i);
+ }
[1] FALSE FALSE FALSE
[1] 1
[1] FALSE FALSE FALSE
[1] 2
[1] FALSE FALSE FALSE
[1] 3
>

But that strangely doesn't work.  I must protest
the implications of the above.  i , as an iterator,
is supposed take on values from b, but never is it equivalent
to any of the values in b.  The above works correctly for 
numbers or arrays of character strings.   What's up with factors?

Thanks for any help.

-Frank




More information about the R-help mailing list