[R] Trying to understand factors
David Winsemius
dwinsemius at comcast.net
Fri Mar 30 19:09:12 CEST 2012
On Mar 30, 2012, at 12:50 PM, Julio Sergio wrote:
>
> 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
I think you need to understand indexing more than you need to
understand factors.
incomes [ which(statef == "act") ]
>
>
> Do you have any comments on this?
If you want to understand how to programmatically access levels, then
you only need to follow the "See also" links on the ?factor page.
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list