[R] Trying to understand factors

Julio Sergio juliosergio at gmail.com
Fri Mar 30 18:50:45 CEST 2012

I'm trying to figure out about factors, however the on-line documentation is 
rather sparse. I guess, factors are intended for grouping arrays members into 
categories, which R names "Levels". And so we have:

 * state <- c("tas", "sa",  "qld", "nsw", "nsw", "nt",  "wa",  "wa",
                  "qld", "vic", "nsw", "vic", "qld", "qld", "sa",  "tas",
                  "sa",  "nt",  "wa",  "vic", "qld", "nsw", "nsw", "wa",
                  "sa",  "act", "nsw", "vic", "vic", "act")
 * statef <- factor(state)
 * statef
 [1] tas sa  qld nsw nsw nt  wa  wa  qld vic nsw vic qld qld sa  tas sa  nt  wa 
 [20] vic qld nsw nsw wa  sa  act nsw vic vic act
 Levels: act nsw nt qld sa tas vic wa

With this, just visually, I know what the cateogries or Levels are. Nonetheless, 
two questions arise here: How can I have, computationally as opposed to 
visually, access to the names of these categories, and how do I get the indexes 
of the original array elements that belong to a particular category, say, "act"?
This is, for instance, to select from another "parallel" array, the 
corresponding elements, say

 * incomes <- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,
                    61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46,
                    59, 46, 58, 43)

So to select, the corresponding elements to "act":

  46 43

Do you have any comments on this?



More information about the R-help mailing list