Skip to content
Prev 155827 / 398503 Next

Newbie: 'table' output in columns rather than matrix

on 09/11/2008 09:34 PM Matthew Pettis wrote:
Welcome!
See ?as.data.frame.table

Example using the 'warpbreaks' dataset:
'data.frame':	54 obs. of  3 variables:
 $ breaks : num  26 30 54 25 70 52 51 26 67 18 ...
 $ wool   : Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
 $ tension: Factor w/ 3 levels "L","M","H": 1 1 1 1 1 1 1 1 1 2 ...
tension
wool L M H
   A 9 9 9
   B 9 9 9
wool tension Freq
1    A       L    9
2    B       L    9
3    A       M    9
4    B       M    9
5    A       H    9
6    B       H    9



So with your data:

  as.data.frame(with(A, table(x, y)))


See ?with also, which enables you to specify the column names 'with[in]'
the environment of the data frame, so that you need not use the '$' syntax.

HTH,

Marc Schwartz