(R-intro.info)Factors


Next: Arrays and matrices Prev: Objects Up: Top
Enter node , (file) or (file)node

4 Ordered and unordered factors
*******************************

A _factor_ is a vector object used to specify a discrete classification
(grouping) of the components of other vectors of the same length.  R
provides both _ordered_ and _unordered_ factors.  While the "real"
application of factors is with model formulae (Note: Contrasts), we
here look at a specific example.

The function tapply() and ragged arrays
Ordered factors
4.1 A specific example
======================

Suppose, for example, we have a sample of 30 tax accountants from all
the states and territories of Australia(1) and their individual state of
origin is specified by a character vector of state mnemonics as

     > 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")

   Notice that in the case of a character vector, "sorted" means sorted
in alphabetical order.

   A _factor_ is similarly created using the 'factor()' function:

     > statef <- factor(state)

   The 'print()' function handles factors slightly differently from
other objects:

     > statef
      [1] tas sa  qld nsw nsw nt  wa  wa  qld vic nsw vic qld qld sa
     [16] tas sa  nt  wa  vic qld nsw nsw wa  sa  act nsw vic vic act
     Levels:  act nsw nt qld sa tas vic wa

   To find out the levels of a factor the function 'levels()' can be
used.

     > levels(statef)
     [1] "act" "nsw" "nt"  "qld" "sa"  "tas" "vic" "wa"

The function tapply() and ragged arrays
Ordered factors
   ---------- Footnotes ----------

   (1) Readers should note that there are eight states and territories
in Australia, namely the Australian Capital Territory, New South Wales,
the Northern Territory, Queensland, South Australia, Tasmania, Victoria
and Western Australia.


automatically generated by info2www version 1.2.2.9