[R] prop.table on three-way table?

Fredrik Karlsson dargosch at gmail.com
Thu Apr 20 10:28:49 CEST 2006


Hi marc,

I did not manage ctab to do this for me. Again, I am probably using it
wrong, but I don't know that the problem is.

You asked for a more illustrated example, so here goes:

Take this table:

> ftable(table(sample(paste("dim1_no",1:5,sep=""),10,replace=TRUE),
sample(paste("dim2_no",1:5,sep=""),10,replace=TRUE),
sample(paste("dim3_no",1:5,sep=""),10,replace=TRUE)))

                   dim3_no1 dim3_no2 dim3_no3 dim3_no5

dim1_no1 dim2_no1         0        0        0        0
         dim2_no2         0        0        2        0
         dim2_no3         0        0        0        0
         dim2_no4         0        0        0        0
         dim2_no5         0        0        0        0
dim1_no2 dim2_no1         0        0        0        0
         dim2_no2         0        0        0        0
         dim2_no3         0        0        0        0
         dim2_no4         0        0        0        0
         dim2_no5         0        1        0        1
dim1_no3 dim2_no1         0        0        0        0
         dim2_no2         0        0        0        0
         dim2_no3         0        0        1        0
         dim2_no4         0        0        0        0
         dim2_no5         0        0        0        0
dim1_no4 dim2_no1         1        0        0        0
         dim2_no2         0        0        0        0
         dim2_no3         0        0        0        0
         dim2_no4         0        0        0        0
         dim2_no5         1        0        0        0
dim1_no5 dim2_no1         0        0        0        0
         dim2_no2         0        1        0        0
         dim2_no3         1        0        0        0
         dim2_no4         0        1        0        0
         dim2_no5         0        0        0        0
>

Now, I would like to get the per cent occurrence of each level of
dim3_noX witin the cells by dim1 and dim2. Thus, for this part section
of the table above:

                   dim3_no1 dim3_no2 dim3_no3 dim3_no5
dim1_no2 dim2_no1         0        0        0        0
         dim2_no2         0        0        0        0
         dim2_no3         0        0        0        0
         dim2_no4         0        0        0        0
         dim2_no5         0        1        0        1

I would like to get:

                   dim3_no1 dim3_no2 dim3_no3 dim3_no5
dim1_no2 dim2_no1         0        0        0        0
         dim2_no2         0        0        0        0
         dim2_no3         0        0        0        0
         dim2_no4         0        0        0        0
         dim2_no5         0        0.5        0        0.5

since dim3_no2 represented 50% of the frequency within the cell
created by dim1_no2 and dim2_no5.


Hope that helped clarify my previous explanation of the problem.

/Fredrik

2006/4/19, Marc Schwartz (via MN) <mschwartz at mn.rr.com>:
> On Wed, 2006-04-19 at 16:39 +0200, Fredrik Karlsson wrote:
> > Dear list,
> >
> > I am trying to create a three-way table with percent occurrence
> > instead of raw frequencies. However, I cannot get the results I
> > expected:
> >
> > I have the following table:
> >
> > > ftable(table( mannerDF$agem, mannerDF$target, mannerDF$manner ))
> > <snip>
> > 50 bak             0         0       0     0       1         0
> >    pak             0         0       0     0       3         0
> >    sak             0         1       0     0       0         0
> >    spak            0         0       0     0       0         0
> >
> > Now, If I use the prop-table function, I newer get a 1 ratio in any cell:
> >
> >
> >
> > With 'margin=1':
> >
> > 50 bak    0.00000000 0.00000000 0.00000000 0.00000000 0.20000000 0.00000000
> >    pak    0.00000000 0.00000000 0.00000000 0.00000000 0.60000000 0.00000000
> >    sak    0.00000000 0.20000000 0.00000000 0.00000000 0.00000000 0.00000000
> >    spak   0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
> >
> > With 'margin=2':
> >
> > 50 bak   0.000000000 0.000000000 0.000000000 0.000000000 0.004347826 0.000000000
> >    pak   0.000000000 0.000000000 0.000000000 0.000000000 0.010752688 0.000000000
> >    sak   0.000000000 0.005747126 0.000000000 0.000000000 0.000000000 0.000000000
> >    spak  0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
> >
> > With 'margin=3':
> >
> > 50 bak   0.000000000 0.000000000 0.000000000 0.000000000 0.001373626 0.000000000
> >    pak   0.000000000 0.000000000 0.000000000 0.000000000 0.004120879 0.000000000
> >    sak   0.000000000 0.008695652 0.000000000 0.000000000 0.000000000 0.000000000
> >    spak  0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
> >
> > What I was looking for is this:
> >
> >
> > 50 bak             0         0       0     0       1         0
> >    pak             0         0       0     0       1         0
> >    sak             0         1       0     0       0         0
> >    spak            0         0       0     0       0         0
> >
> > (With more digits)
> >
> > Am I doing something stupid?
>
> I may be missing what you are trying to do, since we don't have your
> data to reproduce the output. However, you might want to look at the
> ctab() function in the 'catspec' package on CRAN by John Hendrickx.
>
> It builds on the ftable() and prop.table() functions to generate
> formatted n-way percentage tables.
>
> HTH,
>
> Marc Schwartz
>
>
>


--
My Gentoo + PVR-350 + IVTV + MythTV blog is on
http://gentoomythtv.blogspot.com/




More information about the R-help mailing list