[R] xtable.by

Gabor Grothendieck ggrothendieck at gmail.com
Mon Nov 5 20:23:42 CET 2007

If you look at the latex output associated with the page you
posted you need to:

- eliminated all repeated elements in column 1
- replace each non-repeated element, say X, which spans 3 rows, say, to
- add an \hline after the last row

Using the first 10 rows of the builtin Orange data frame try the
following.  Adjust align= appropriately for your case.

DF <- head(Orange, 10)
DF[[1]] <- as.character(DF[[1]])

rle.lengths <- rle(DF[[1]])$lengths
first <- !duplicated(DF[[1]])

DF[[1]][!first] <- ""
DF[[1]][first] <-
   paste("\\hline\\multirow{", rle.lengths, "}{*}{", DF[[1]][first], "}")

print(xtable(DF, align = "l|r|r|r|"),
   include.rownames = FALSE, include.colnames = FALSE,
   sanitize.text.function = force,
   hline.after = nrow(DF)

On Nov 5, 2007 11:31 AM, A Friedman <abfriedman at gmail.com> wrote:
> Hi all,
>  I have a series of measurements made over different types of road surfaces
> and capacity that I want to summarize in tables via LaTeX.  Ideally I'd like
> a table similar to this (
> http://www.andy-roberts.net/misc/latex/tutorial4/multirow.png ) with mean/SD
> of the measurement broken down by road type (both surface and capacity).
> Ideally it would be extensible so that when I add layers of heirarchy on top
> the complexity does not increase exponentially (e.g. measurement by road
> surface/capacity by month by neighborhood), but rather take those additional
> levels as arguments.
> xtable() is a wonderful thing, but it seems to lack a method for displaying
> the results broken down by a factor as in the table above.  One can easily
> write code to add lines between the segments, but then the levels are
> repeated for each row.  Has anyone written a method for xtable() to handle
> this (assuming not, otherwise it'd be in the package)?  Is there any way to
> pass a \multirow call to xtable() (assuming not, otherwise it'd be in the
> documentation)?
> If the answers to both of the above are "no," then can someone give me a
> pointer to a basic but thorough explanation of how generic functions and
> methods are structured (S3, I believe), preferably online.  I tried doing
> this via getS3method("xtable","data.frame") and working from there, but the
> method merely sets up things for the generic function to render (it's likely
> I'm getting my terminology somewhat wrong here; please forgive) and what I
> want to do is beyond the bounds of modifying it in this way.  I've looked
> through _Writing R Extensions_, but the Generic Functions and Methods
> chapter is only about a page long.
> Thanks on behalf of all the lurkers.  I've learned much just by reading the
> daily digests over the last few weeks.
> Ari
> Ari Friedman
> abfriedman at gmail.com
>        [[alternative HTML version deleted]]
> ______________________________________________
> 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.

More information about the R-help mailing list