[R] Are you sure a table can become a data.frame?

Gabor Grothendieck ggrothendieck at gmail.com
Sun Nov 16 20:46:29 CET 2008


Try:

as.data.frame.matrix(table(X))


On Sun, Nov 16, 2008 at 1:28 PM, CE.KA <ce.kaya75 at yahoo.fr> wrote:
>
> Hi R users,
>
> Imagine X as a data frame:
>
> X=read.table(textConnection("
> Var1 Var2
> 1 A H
> 2 B K
> 3 A H
> 4 B H
> 5 C L
> "),header=TRUE)
> closeAllConnections()
>
> X                                                X is a data frame
>  Var1 Var2
> 1    A    H
> 2    B    K
> 3    A    H
> 4    B    H
> 5    C    L
>
> Y<-table(X$Var1,X$Var2)         Y is a cross table between Var1 and Var2
>                                                  I want Y to become a data
> frame
>
> Y
>   H K L
>  A 2 0 0
>  B 1 1 0
>  C 0 0 1
>
> Z<-as.data.frame(Y)                    I want Y to become a data frame: it
> doesn't work
> Z
>  Var1 Var2 Freq
> 1    A    H    2
> 2    B    H    1
> 3    C    H    0
> 4    A    K    0
> 5    B    K    1
> 6    C    K    0
> 7    A    L    0
> 8    B    L    0
> 9    C    L    1
>
> I expect Z as following data frame
> Z
>   H K L
>  A 2 0 0
>  B 1 1 0
>  C 0 0 1
>
> I tried as.data.frame(array(x, dim(x), dimnames(x)))  and
> as.data.frame.table
> but it doesn'work
> Kind regards
>
> --
> View this message in context: http://www.nabble.com/Are-you-sure-a-table-can-become-a-data.frame--tp20528308p20528308.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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