[R] How to convert an ftable object to a matrix including the row names?
Dennis Murphy
djmuser at gmail.com
Fri May 27 10:24:48 CEST 2011
Hi:
Use can use dcast() from the reshape2 package:
library(reshape2)
> dcast(ft.df, Class + Sex + Age ~ Survived)
Using Freq as value column: use value_var to override.
Class Sex Age No Yes
1 1st Male Child 0 5
2 1st Male Adult 118 57
3 1st Female Child 0 1
4 1st Female Adult 4 140
5 2nd Male Child 0 11
6 2nd Male Adult 154 14
7 2nd Female Child 0 13
8 2nd Female Adult 13 80
9 3rd Male Child 35 13
10 3rd Male Adult 387 75
11 3rd Female Child 17 14
12 3rd Female Adult 89 76
13 Crew Male Child 0 0
14 Crew Male Adult 670 192
15 Crew Female Child 0 0
16 Crew Female Adult 3 20
Adding value = 'Freq' to the argument list of dcast() will get rid of
the warning message on the console.
HTH,
Dennis
On Thu, May 26, 2011 at 11:54 PM, Marius Hofert <m_hofert at web.de> wrote:
> Dear Prof. Ripley,
>
> many thanks for your quick reply.
>
> A character matrix (although clearly not very elegant) would be no problem, xtable deals
> with that.
> I tried as.data.frame() before, but if one wants to have the same rows
> as in ft, one has to use additional commands (?):
> ft # => 16 rows
> as.data.frame(ft) # => 32 rows; different order
> Is there a simple way to get the same order of the variables as in ft?
>
> Cheers,
>
> Marius
>
> On 2011-05-27, at 08:17 , Prof Brian Ripley wrote:
>
>> as.data.frame(ft)
>>
>> seems straightforward enough.
>>
>> I don't think you actually want a matrix, as they would have to be a character matrix and the ftable object is numeric.
>>
>> On Fri, 27 May 2011, Marius Hofert wrote:
>>
>>> 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.
>>>
>>
>> --
>> Brian D. Ripley, ripley at stats.ox.ac.uk
>> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
>> University of Oxford, Tel: +44 1865 272861 (self)
>> 1 South Parks Road, +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK Fax: +44 1865 272595
>
> ______________________________________________
> 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