Skip to content
Prev 43468 / 398506 Next

looping over factors

mmiller3 at iupui.edu (Michael A. Miller) writes:
I don't think that will be what is intended.  The levels attribute is
the distinct character representations of values in the factor.  It
need not be the same length as the factor itself.
myfac
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
6 2 4 5 2 3 7 6 3 7 6 3 8 3 1 3 3 5 2 4 2 2 5 4 2 2
[1] 26

By default the levels attribute is sorted lexicographically.
[1] "A"
[1] "B"
[1] "C"
[1] "D"
...
[1] "Y"
[1] "Z"

To iterate over the elements of myfac as character strings, use
as.character
[1] "X"
[1] "L"
[1] "M"
[1] "W"
[1] "W"
...
[1] "E"
[1] "G"
[1] "H"

Hope this helps.