I have a table (output from table(factor1,factor2)). I would like to use write.table to output that table to a file. However, it seems that as.data.frame converts such a table to three columns, Var1, Var2, and Freq rather than converting to the data.frame with equivalent numbers of rows and columns. I can use write.matrix from the MASS package, but then I get no rownames. Any hints here? Thanks Sean
Here's one way:> f1 <- factor(rep(1:2, e=10)) > f2 <- factor(rep(1:2, 10)) > tab <- table(f1, f2) > str(as.data.frame(unclass(tab)))`data.frame': 2 obs. of 2 variables: $ 1: int 5 5 $ 2: int 5 5 HTH, Andy> From: Sean Davis > > I have a table (output from table(factor1,factor2)). I would like to > use write.table to output that table to a file. However, it > seems that > as.data.frame converts such a table to three columns, Var1, Var2, and > Freq rather than converting to the data.frame with equivalent numbers > of rows and columns. I can use write.matrix from the MASS > package, but > then I get no rownames. Any hints here? > > Thanks > Sean > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > R-project.org/posting-guide.html > >
Why not use sink()? On Thu, 28 Oct 2004, Sean Davis wrote:> I have a table (output from table(factor1,factor2)). I would like to > use write.table to output that table to a file. However, it seems that > as.data.frame converts such a table to three columns, Var1, Var2, and > Freq rather than converting to the data.frame with equivalent numbers > of rows and columns. I can use write.matrix from the MASS package, but > then I get no rownames. Any hints here?-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, 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
Sean Davis <sdavis2 <at> mail.nih.gov> writes: : I have a table (output from table(factor1,factor2)). I would like to : use write.table to output that table to a file. However, it seems that : as.data.frame converts such a table to three columns, Var1, Var2, and : Freq rather than converting to the data.frame with equivalent numbers : of rows and columns. I can use write.matrix from the MASS package, but : then I get no rownames. Any hints here? You could use captureOutput like this: data(HairEyeColor) capture.output(HairEyeColor, file = "myfile.txt") See ?capture.output for more possibilities.
Reasonably Related Threads
- Obtaining the variable names of a glm object
- How to get minimum value by group
- Ggplot2: Moving legend, change fill and removal of space between plots when using grid.arrange() possible use of facet_grid?
- ggpliot2: reordering of factors in facets facet.grid(). Reordering of factor on x-axis no problem.
- reference