[R] ftable and data.frame

David Winsemius dwinsemius at comcast.net
Fri Dec 20 18:42:01 CET 2013


On Dec 20, 2013, at 5:01 AM, <silvano at uel.br> wrote:

> Hi,
> 
> I used this command to produce a table:

It's not actually a 'table'.

> 
> (tab1 = ftable(SEX, ESTCIV, Q1))

is.table(tab1) # will return FALSE

> 
>                               Q1  B  L  M  N
> SEXO    ESTCIV
>        F           A         11 13  4  2
>                      E          1  0  0  0
>        M           A          5  0  3  1
>                      E          0  0  0  0
> 
> but I need something like:
> 
> 
> SEXO    ESTCIV        B  L  M  N
>      F            A         11 13  4  2
>      F            E          1  0  0  0
>      M           A          5  0  3  1
>      M           E          0  0  0  0
> 
> How can I get it?

You need to describe the purpose of this effort. 

If is for display, the answer will be to look at the code for `print.ftable` (which eventually passes its arguments to `format.ftable`.

If it is for creating something other than an ftable (such a data.frame or character-matrix), then you will probably need to coerce the ftable to a matrix to get the entry values and then extract the row labels from the 'tab1'-object with:   attr(tab1, "row.vars") and `rep` them approriately. Or you could work with the output from format(ftable(tab1))[ ,1:2].

cbind( format(ftable(Titanic), quote=FALSE)[ ,1:3], 
       format(ftable(Titanic), quote=FALSE)[ ,5:6])
      [,1]    [,2]     [,3]    [,4]  [,5] 
 [1,] "     " "      " "     " " No" "Yes"
 [2,] "Class" "Sex   " "Age  " "   " "   "
 [3,] "1st  " "Male  " "Child" "  0" "  5"
 [4,] "     " "      " "Adult" "118" " 57"
 [5,] "     " "Female" "Child" "  0" "  1"
 [6,] "     " "      " "Adult" "  4" "140"
 [7,] "2nd  " "Male  " "Child" "  0" " 11"
 [8,] "     " "      " "Adult" "154" " 14"
 [9,] "     " "Female" "Child" "  0" " 13"
[10,] "     " "      " "Adult" " 13" " 80"
[11,] "3rd  " "Male  " "Child" " 35" " 13"
[12,] "     " "      " "Adult" "387" " 75"
[13,] "     " "Female" "Child" " 17" " 14"
[14,] "     " "      " "Adult" " 89" " 76"
[15,] "Crew " "Male  " "Child" "  0" "  0"
[16,] "     " "      " "Adult" "670" "192"
[17,] "     " "Female" "Child" "  0" "  0"
[18,] "     " "      " "Adult" "  3" " 20"

> 
> I need this format to use ordinal logistic regression and I have many tables.

That makes me think using ftable is the completely misguided approach. You should describe in more detail you plans to use ordinal logistic regression. I'm also wondering if you have used attach(). It would make more sense to keep these variables in a dataframe. Almost all regression functions expect input in the form of a normalized dataframe rather than in separate variables of aggregated counts.


> (tab1 = ftable(SEX, ESTCIV, Q1))
> 
> Thanks,
> 
> Silvano.
> 
> ---
> Este email está limpo de vírus e malwares porque a proteção do avast! Antivírus está ativa.
> 
> 
> 	[[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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list