[R] xtable with a table ?

Ian Wilson I.Wilson at maths.abdn.ac.uk
Tue May 11 16:41:54 CEST 2004


Any more elegant solutions to this?

> a <- sample(c("a","d","c"),100,replace=T)
> b <- sample(c("d","e","f",100,replace=T)
> t <- table(a,b)
> xtable(t)
Error in xtable(t) : no applicable method for "xtable"

The problem is that while t is a table (and
hence also a matrix)

> is.matrix(t)
[1] TRUE

 data.frame(t) produces

> data.frame(t)
  a b Freq
1 1 1   12
2 2 1   12
3 3 1    7
4 1 2    8
5 2 2   12
6 3 2   11
7 1 3   13
8 2 3   17
9 3 3    8

After a horrible solution, I have 
ct <- apply(t,2,cbind);
rownames(ct) <- rownames(t);
xtable(ct)

Is this a bug/feature, and if so how do I get round it?

Ian Wilson




More information about the R-help mailing list