[R] Convert ftable to latex?

Fredrik Karlsson dargosch at gmail.com
Tue Aug 30 15:36:34 CEST 2005


Thank you so much David. 

That example provided me with enough information for me to convert the
table to latex, with
subdivisions intact.

/Fredrik

On 8/30/05, David Whiting <david.whiting at ncl.ac.uk> wrote:
> Hi Fredrik,
> 
> What you need to do is to massage your table a little to get it into the
> appropriate structure and then use the rgroup and n.rgroup options.
> Here's an example using the Titanic data that come with R. Note that
> this is not necessarily (or even remotely likely?) the best way to get
> your data into the shape required, but hopefully it shows you what, in
> general, you need to do. You might find that you don't need to use
> ftable at all, you might find other ways of creating the table you need.
> In the example below, note in particular what x and x2 look like.
> 
> > library(Hmisc)
> > x <- ftable(Titanic, row.vars = 1:2)
> > x
>              Age      Child     Adult
>              Survived    No Yes    No Yes
> Class Sex
> 1st   Male                0   5   118  57
>       Female              0   1     4 140
> 2nd   Male                0  11   154  14
>       Female              0  13    13  80
> 3rd   Male               35  13   387  75
>       Female             17  14    89  76
> Crew  Male                0   0   670 192
>       Female              0   0     3  20
> > x.row.vars <- attr(x, "row.vars")
> > col1 <- x.row.vars[[1]]
> > col2 <- rep(x.row.vars[[2]], 4)
> > x2 <- data.frame(sex=col2, x[ ,])
> > colnames(x2) <- c("Sex", "No", "Yes", "No", "Yes")
> > x2
>      Sex No Yes  No Yes
> 1   Male  0   5 118  57
> 2 Female  0   1   4 140
> 3   Male  0  11 154  14
> 4 Female  0  13  13  80
> 5   Male 35  13 387  75
> 6 Female 17  14  89  76
> 7   Male  0   0 670 192
> 8 Female  0   0   3  20
> > latex(x2,
> +       title="",
> +       rowname="",
> +       rgroup=col1,
> +       cgroup=c("", "Child", "Adult"),
> +       n.cgroup=c(1, 2, 2),
> +       n.rgroup=rep(2, 4)
> +       )
> 
> 
> HTH.
> 
> Dave
> 
> 
> Fredrik Karlsson wrote:
> > Dear list,
> >
> > I cannot make the latex command to output a ftable objet the way I
> > want it. Is it posible?
> > I found a post in the archives saying that one should use the rgroup
> > and n.rgroup arguments to supply the row names, but so far I have been
> > unsuccessful.
> >
> > This is what I have:
> >
> >
> >>(ftable(tapply(worksub$vot,list(votcat=worksub$votcat,age=worksub$agemF,voicetype=worksub$Type),FUN="distribution.table.fun",digits=4)) -> ftab)
> >
> >                   voicetype                  Voiced   Voiceless
> > unaspirated     Voiceless aspirated
> > votcat    age
> > Prevoiced 18 - 24           46.6158 (0;-1.6652)     40.7417
> > (0;-0.6489)     48.4164 (0;-1.0483)
> >           24 - 30           50.5716 (0;-1.4244)**   43.4056
> > (-1;-0.4537)*** 24.204 (0;-2.1416)
> >           30 - 36           44.4439 (0;-1.182)*     51.0996
> > (0;-1.5241)***  32.1219 (0;-1.5007)
> >           36 - 42           40.8604 (-1;-0.3423)    40.6045
> > (-1;-0.408)**   32.7949 (0;-2.75)
> >           42 - 48           46.301 (0;-1.1878)      21.6894
> > (0;-1.7041)     NA (NA;NA)
> >           48 - 54           38.0151 (-1;-0.7878)*   27.6954
> > (-1;0.0396)*    NA (NA;NA)
> > Short lag 18 - 24           7.5719 (1;0.4391)***    9.7039
> > (1;-0.2938)***   8.5525 (1;-0.4063)***
> >           24 - 30           8.3466 (1;-0.3122)***   9.8524
> > (0;-0.887)***    11.4154 (0;-1.2267)***
> >           30 - 36           9.4509 (1;-0.0795)***   9.0177
> > (1;-0.2654)***   9.441 (0;-0.7625)
> >           36 - 42           9.4921 (1;-0.1835)***   10.107 (0;-0.72)**
> >      10.912 (0;-1.3619)
> >           42 - 48           7.8254 (1;1.016)**      9.5687 (0;-0.9019)
> >      10.6842 (-1;0.0719)*
> >           48 - 54           7.7332 (1;1.2834)**     9.4626
> > (1;0.3173)***    10.0508 (0;-1.4876)
> > Long lag  18 - 24           16.7312 (0;-1.7286)     21.4786
> > (2;2.4726)**    41.6646 (1;-0.6796)***
> >           24 - 30           29.5637 (1;-0.0951)**   37.4517
> > (1;0.1032)***   38.2729 (1;-0.3249)***
> >           30 - 36           23.0214 (0;-1.3023)     35.0403
> > (1;0.9176)***   36.0989 (1;-0.2141)***
> >           36 - 42           10.579 (1;0.3292)       31.4878
> > (0;-1.2475)     38.0472 (1;-0.3049)**
> >           42 - 48           17.9077 (1;-1.2857)     26.8651
> > (1;0.0221)***   30.5705 (1;-0.5866)***
> >           48 - 54           18.832 (0;-2.3333)      40.375 (1;-1.417)*
> >      26.2463 (1;0.4025)***
> >
> >>latex(ftab,cgroup=attributes(ftab)$col.vars$voicetype, rgroup=attributes(ftab)$row.vars$votcat, n.rgroup=c(6,6,6),file="")
> >
> > % latex.default(ftab, cgroup = attributes(ftab)$col.vars$voicetype,
> >   rgroup = attributes(ftab)$row.vars$votcat, n.rgroup = c(6,
> > 6, 6), file = "")
> > %
> > \begin{table}[!tbp]
> >  \begin{center}
> >  \begin{tabular}{lclcl}\hline\hline
> > \multicolumn{1}{c}{\bfseries Voiced}&
> > \multicolumn{1}{c}{\bfseries }&
> > \multicolumn{1}{c}{\bfseries Voiceless unaspirated}&
> > \multicolumn{1}{c}{\bfseries }&
> > \multicolumn{1}{c}{\bfseries Voiceless aspirated}
> > \\ \cline{1-5}
> > \multicolumn{1}{c}{}&
> > \multicolumn{1}{c}{}&
> > \multicolumn{1}{c}{}&
> > \multicolumn{1}{c}{}&
> > \multicolumn{1}{c}{}
> > \\ \hline
> > &&&&\\
> > 46.6158 (0;-1.6652)&&40.7417 (0;-0.6489)&&48.4164 (0;-1.0483)\\
> > 50.5716 (0;-1.4244)**&&43.4056 (-1;-0.4537)***&&24.204 (0;-2.1416)\\
> > 44.4439 (0;-1.182)*&&51.0996 (0;-1.5241)***&&32.1219 (0;-1.5007)\\
> > 40.8604 (-1;-0.3423)&&40.6045 (-1;-0.408)**&&32.7949 (0;-2.75)\\
> > 46.301 (0;-1.1878)&&21.6894 (0;-1.7041)&&NA (NA;NA)\\
> > 38.0151 (-1;-0.7878)*&&27.6954 (-1;0.0396)*&&NA (NA;NA)\\
> > \hline
> > &&&&\\
> > 7.5719 (1;0.4391)***&&9.7039 (1;-0.2938)***&&8.5525 (1;-0.4063)***\\
> > 8.3466 (1;-0.3122)***&&9.8524 (0;-0.887)***&&11.4154 (0;-1.2267)***\\
> > 9.4509 (1;-0.0795)***&&9.0177 (1;-0.2654)***&&9.441 (0;-0.7625)\\
> > 9.4921 (1;-0.1835)***&&10.107 (0;-0.72)**&&10.912 (0;-1.3619)\\
> > 7.8254 (1;1.016)**&&9.5687 (0;-0.9019)&&10.6842 (-1;0.0719)*\\
> > 7.7332 (1;1.2834)**&&9.4626 (1;0.3173)***&&10.0508 (0;-1.4876)\\
> > \hline
> > &&&&\\
> > 16.7312 (0;-1.7286)&&21.4786 (2;2.4726)**&&41.6646 (1;-0.6796)***\\
> > 29.5637 (1;-0.0951)**&&37.4517 (1;0.1032)***&&38.2729 (1;-0.3249)***\\
> > 23.0214 (0;-1.3023)&&35.0403 (1;0.9176)***&&36.0989 (1;-0.2141)***\\
> > 10.579 (1;0.3292)&&31.4878 (0;-1.2475)&&38.0472 (1;-0.3049)**\\
> > 17.9077 (1;-1.2857)&&26.8651 (1;0.0221)***&&30.5705 (1;-0.5866)***\\
> > 18.832 (0;-2.3333)&&40.375 (1;-1.417)*&&26.2463 (1;0.4025)***\\
> > \hline
> > \end{tabular}
> >
> > \end{center}
> >
> > \end{table}
> >
> >
> >
> > As you can see, I do not get any row names, and I want two layers of
> > them (votcat and age in the formula). Column names work ok.
> >
> > Please, help me recreate the table above in latex form (if it is
> > indeed possible).
> >
> > /Fredrik
> >
> > ______________________________________________
> > R-help at 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
> >
> 
> --
> David Whiting
> School of Clinical Medical Sciences, The Medical School
> University of Newcastle upon Tyne, NE2 4HH, UK.
> 
> "I love deadlines. I love the whooshing noise they make as they go by"
> (Douglas Adams)
> 
> 
> 


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




More information about the R-help mailing list