# Re: [S] Conditioning in Tables

Zhaorong Jiao (jiao@laplace.math.sci.qut.edu.au)
Wed, 01 Jul 1998 11:16:09 +1000 (EST)

one way to your problem may be like this:
if your table function is defined like yourtablefunction(x) , where x is
a vector, then apply(yourmatrix, 1, yourtablefunction) will do the job for
you. Make sure that yourtablefunction has only one argument this time,
otherwise the apply function may not work.....Good luck.
P.S. Functions like apply, sapply, and tapply are allowed user-defined
funtions as their arguments.

On Tue, 30 Jun 1998, Ross Darnell wrote:

> I have been looking for a way to generate conditional tables based on
> elements of data frame.
>
> As an example
>
> I can generate a table, Tab,
>
> > Tab
>
> , , 1
> 1 2 3 4
> 1 0 0 0 0
> 2 0 0 0 0
> 3 0 0 0 0
> 4 0 0 0 0
> 5 0 0 0 0
> 6 1 0 0 0
> 7 0 0 0 1
>
> , , 2
> 1 2 3 4
> 1 0 0 1 0
> 2 0 1 0 0
> 3 0 0 0 0
> 4 0 0 0 0
> 5 1 0 0 0
> 6 0 0 0 0
> 7 0 0 0 0
>
> generated from a data frame, X,
>
> > X
> X1 X2 X3
> 1 3 NA 1
> 2 6 1 1
> 3 NA 2 1
> 4 7 4 1
> 5 4 NA 1
> 6 1 3 2
> 7 2 2 2
> 8 NA NA 2
> 9 NA 2 2
> 10 5 1 2
>
> I would like, as an example, the conditional table of X\$X1 given
> X\$X2=2 and X\$X3=2, which correponds to the 9th row of X (NA,2,2).
> This can be done manually simply by subscripting the array Tab.
>
> > Tab[ ,2,2]
> 1 2 3 4 5 6 7
> 0 1 0 0 0 0 0
>
> Another example correpsonding to row 8 of X (NA,NA,2) is
>
> > Tab[,,2]
> 1 2 3 4
> 1 0 0 1 0
> 2 0 1 0 0
> 3 0 0 0 0
> 4 0 0 0 0
> 5 1 0 0 0
> 6 0 0 0 0
> 7 0 0 0 0
>
> which is the conditional joint table for X1 and X2 given X3=2.
>
> The problem is how can I write a program to use a row of the data
> matrix X as input into the table function like this
>
> Tab[X[9,]]
>
> Mind you this may be the absolutely wrong approach to the problem.
> Any reasonable approach would be welcomed.
>