Re: [R] how to get such crosstable?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 24 Jun 2005 - 06:59:41 EST

On 6/23/05, Marc Schwartz <MSchwartz@mednetstudy.com> wrote:
> On Thu, 2005-06-23 at 23:22 +0800, ronggui wrote:
> > i use the CrossTable (frome gregmic package) function to get such a
> > table as below.
> > but the percentage of the non-NA levels(here 1,2,3,4,5) is not totally
> > 100%.
> >
> > is there any way to get a table that percentage of the non-NA
> > levelsis totally 100%,as the SPSS' valid percentage.thank you!
> >
> >
> >
> > Cell Contents
> > |-------------------------|
> > | Count |
> > | Row Percent |
> > |-------------------------|
> >
> > Total Observations in Table: 650
> >
> > | 1 | 2 | 3 | 4 | 5
> > |
> >
> > |-----------|-----------|-----------|-----------|-----------|
> > | 169 | 294 | 151 | 31 | 5
> > |
> > | 0.260% | 0.452% | 0.232% | 0.048% | 0.008%
> > |
> >
> > |-----------|-----------|-----------|-----------|-----------|
> >
> > Number of Missing Observations: 4 (0.6116208%)
>
>
> I may be misunderstanding what you are referring to, but I am guessing
> that it is that the output is showing proportions, even thought the "%"
> symbol is there?
>
> I am not familiar with SPSS' output (Dirk Enzmann contributed the SPSS
> output format code to CrossTable), but a quick look suggests that it
> works properly in a 2d table, but there is a bug when the object to be
> tabulated is 1d, in that the multiplication by 100 is not done in the
> SPSS output sub-function.
>
> The ability to tabulate a 1d object was added independent of Dirk's
> code, so it looks as if this was missed.
>
> If correct, I can review that and include that as an update, along with
> the prior modification to Greg and Nitin.
>

In the interim, here is a workaround. The function SPSSCrossTable below defines

Finally the do.call runs the local CrossTable:

SPSSCrossTable <- function(...) {

	args <- list(...)
	args$format <- "SPSS"
	prop.table <- function(...) 100*base::prop.table(...)
	environment(CrossTable) <- environment()
	do.call("CrossTable", args)

}
SPSSCrossTable(c(1,1,2,NA)) # test

Note that the above should only be used in cases where the proportions in CrossTable are incorrectly not multiplied by 100.



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Jun 24 07:02:39 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:01 EST