On Sun, 30 Nov 2008, Andrew Choens wrote:
> I have a dataframe with the following variables:
>
> idnum area gender race etc.
>
> I would like to make a table that looks like
>
> area gender race
> M F B W A
> 1 4 5 3 5 1
> 2 6 7 4 6 3
>
> etc.
>
> Basically, I want to make a single broad table with a number of sub-set
> tables. I have tried:
Well you are asked to "provide commented, minimal, self-contained,
reproducible code", and I see nothing here I can reproduce.
But here is a start:
> tab1 <- xtabs(~agegp+tobgp,esoph)
> tab2 <- xtabs(~agegp+alcgp,esoph)
> cat(paste(capture.output(tab1),capture.output(tab2),'\n'))
tobgp alcgp
agegp 0-9g/day 10-19 20-29 30+ agegp 0-39g/day 40-79 80-119 120+
25-34 4 4 3 4 25-34 4 4 3 4
35-44 4 4 4 3 35-44 4 4 4 3
45-54 4 4 4 4 45-54 4 4 4 4
55-64 4 4 4 4 55-64 4 4 4 4
65-74 4 4 4 3 65-74 4 3 4 4
75+ 4 4 1 2 75+ 3 4 2 2
It is left as an exercise to figure out how to redo the first line to move
'alcgp' over ' 0-3' and how to delete the redundant 'agegp;
column. See
?nchar
?substring
and maybe
?sprintf
for some pointers.
HTH,
Chuck
>
>
> cbind(table(area, gender), table(area, race))
>
> But, when I do this, I lose the labels gender / race. This makes it a
> lot harder to understand my factor labels. when I use cbind, I get this:
>
> M F B W A
> 1 4 5 3 5 1
> 2 6 7 4 6 3
>
> Although, it is technically correct, I really want to keep my factor
> labels. I also tried this with xtabs and get the same results. Any
> ideas? I saw a relatively recent thread asking a similar question, but
> the proposed solution did not work for me, so I thought I would ask the
> questions again.
>
> If I am missing someting terribly obvious, I apologize.
>
> thanks.
>
>
> --
> Insert something humorous here. :-)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901