[R] Trying to understand factors

Sarah Goslee sarah.goslee at gmail.com
Fri Mar 30 19:02:56 CEST 2012


Hi Julio,

You can use a factor to index another object just as you'd use any other index:
> incomes[statef == "act"]
[1] 46 43

It looks like you're using the R intro guide, but there's a lot of
other material available. Try this one for starters:
http://www.stat.berkeley.edu/classes/s133/factors.html

Is there something specific you're trying to accomplish?

Sarah

On Fri, Mar 30, 2012 at 12:50 PM, Julio Sergio <juliosergio at gmail.com> 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
>
>
> Do you have any comments on this?
>
> Thanks,
>
> --Sergio.
>

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list