[R] RE How to convert an ftable object to a matrix including the row names?

Marius Hofert m_hofert at web.de
Fri May 27 17:44:19 CEST 2011


Okay, great, thanks :-)

Marius

On 2011-05-27, at 10:56 , david.hajage at curie.net wrote:

> 
> Hi, 
> 
> Is this what you want ? 
> 
> ft <- ftable(Titanic, row.vars=1:3) 
> xtable(format(ft)) 
> 
> Best, 
> 
> david 
> 
> 
> Marius Hofert <m_hofert at web.de> 
> Envoyé par : r-help-bounces at r-project.org
> 27/05/2011 07:41
> 
> A
> Help R <r-help at stat.math.ethz.ch>
> cc
> Objet
> [R] How to convert an ftable object to a matrix including the row        names?
> 
> 
> 
> 
> 
> Dear expeRts,
> 
> What's the easiest way to convert an ftable object to a matrix such that the 
> row names of the ftable object are shown in the first couple of columns of the
> matrix? This is (typically) required, for example, when the final goal is to print 
> the matrix via xtable.
> 
> Below is a rather complicated example of how to do it... 
> 
> Cheers,
> 
> Marius
> 
> ## Goal: convert an ftable() to a (character) matrix including the row names of 
> ##       the ftable object as columns in the matrix (so that the matrix can be
> ##       nicely printed with xtable() for example)
> (ft <- ftable(Titanic, row.vars=1:3)) # ftable object
> rn <- attr(ft, "row.vars") # pick out rownames
> rn. <- rn[length(rn):1] # unfortunately, we have to (?) change the order due to expand.grid()
> g <- expand.grid(rn.) # build the 3 columns containing the row names
> (g. <- g[,length(rn):1]) # change order back; now contains the same row names as ft
> (ft.mat <- as.matrix(ft)) # convert ftable object to a matrix
> ## now, cbind g. and ft.mat
> cbind(g., ft.mat) # => now the rownames are there twice! ... although dim(ft.mat)==c(16, *2*)
> ## class(g.) => okay, probably we meant:
> (res <- cbind(as.matrix(g.), ft.mat))
> require(xtable)
> xtable(res)
> ______________________________________________
> 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.
> 
> 
> L'intégrité de ce message n'étant pas assurée sur Internet, l'Institut Curie ne peut être tenu responsable de son contenu. 
> Si vous n'êtes pas destinataire de ce message confidentiel, merci de le détruire et d'avertir immédiatement l'expéditeur.
> Afin de contribuer au respect de l'environnement, merci de n'imprimer ce mail qu'en cas de nécessité.



More information about the R-help mailing list