A Specific Example

Suppose, for example, we have a sample of 30 tax accountants from the all states and territories[*] 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

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"



Jeff Banfield
2/13/1998