[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

More Re: cross tables in reports?

From: Stanley A. Klein
Subject: More Re: cross tables in reports?
Date: Fri, 01 Nov 2002 13:18:50

Below is an extract from the R Introduction manual discussing crosstabs.
In addition to "table," there is also a function "xtab" (I'm not sure where
it is described).

BTW, to find the links to R on, it is best to go to the
directory of free software and click on "all software in directory."

Stan Klein


5.10 Frequency tables from factors

Recall that a factor defines a partition into groups. Similarly a pair of
factors defines a two way cross classification, and so on. The function
table() allows frequency tables to be calculated from equal length factors.
If there are k category arguments, the result is a k-way array of
frequencies. Suppose, for example, that statef is a factor giving the state
code for each entry in a data vector. The assignment

> statefr <- table(statef)

gives in statefr a table of frequencies of each state in the sample. The
frequencies are ordered and labelled by the levels attribute of the
category. This simple case is equivalent to, but more convenient than,

> statefr <- tapply(statef, statef, length)

Further suppose that incomef is a category giving a suitably defined
“income class” for each entry in the data vector, for example with the
cut() function:

> factor(cut(incomes, breaks = 35+10*(0:7))) -> incomef

Then to calculate a two-way table of frequencies:
> table(incomef,statef) 


incomef act nsw nt qld sa tas vic wa
(35,45] 1 1 0 1 0 0 1 0
(45,55] 1 1 1 1 2 0 1 3
(55,65] 0 3 1 3 2 2 2 1
(65,75] 0 1 0 0 0 0 1 0

Extension to higher way frequency tables is immediate.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]