tags:

views:

66

answers:

1

Take for example the following ftable

height <- c(rep('short', 7), rep('tall', 3))
girth <- c(rep('narrow', 4), rep('wide', 6))
measurement <- rnorm(10)
foo <- data.frame(height=height, girth=girth, measurement=measurement)
ftable.result <- ftable(foo$height, foo$girth)

I'd like to convert the above ftable.result into a matrix with row names and column names. Is there an efficient way of doing this? as.matrix() doesn't exactly work, since it won't attach the row names and column names for you.

You could do the following

ftable.matrix <- ftable.result
class(ftable.matrix) <- 'matrix'

rownames(ftable.matrix) <- unlist(attr(ftable.result, 'row.vars'))
colnames(ftable.matrix) <- unlist(attr(ftable.result, 'col.vars'))

However, it seems a bit heavy-handed. Is there a more efficient way of doing this?

+1  A: 

I found 2 solutions on R-Help:

head(as.table(ftable.result), Inf)

Or

t <- as.table(ftable.result)
class(t) <- "matrix"
Shane
I didn't know that you could use `as.table` in that fashion.
andrewj