[R] table () for more variables

David Winsemius dwinsemius at comcast.net
Sat Jul 4 16:24:52 CEST 2009


On Jul 3, 2009, at 1:10 PM, John Lipkins wrote:

> Dear  All,
>
> I want to create a table for several variables. As example. I have a
> dataframe with following data:
>
> Gender            transport      driving
> 1                     0                 1
> 0                     1                 0
> 1                     0                 1
>
> Now I want to create a table in the following form:

  gtd <- read.table(textConnection("Gender            transport       
driving
  1                     0                 1
  0                     1                 0
  1                     0                 1
  "), header=TRUE)

>
>                                   Gender
>                                  1         0
> Transport 1                  2         0
>               0                 0         1
> Driving     1                  2         0
>               0                  0         1

That's really two tables stacked on top of each other. The sum of its  
entries is 2n.

with(gtd, rbind(
                 xtabs( ~ transport + Gender) ,
                 xtabs( ~ driving + Gender)
        )       )
   0 1
0 0 2
1 1 0
0 1 0
1 0 2
>
> In which the different percentages are being calculated (row/column).

Percentages? Don't see any in the requested output. Maybe you really  
want CroosTables in the gmodels package.

> I have
> tried using ftable() but did not give the desired result (There are  
> a lot of
> variables and the format makes it impossible to interpret).

You could think about using summary or one of its substitutes inside  
an apply construction.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list