Vumani Dlamini
2007-Jun-20 09:20 UTC
[R] "xtable" results doesn't correspond to data.frame
Dear useRs, Am trying to use xtable on the following data.frame and I don't get what I expect: example.table <- data.frame(rbind( c("Gender"," "," "," "), cbind(rep(" ",2),c("Male","Female"),c(3.0,4.0),c(3/7,4/7)) )) colnames(example.table) <- c(" "," ","number of patients","%") example.xtable <- xtable(example.table) print.xtable(example.xtable, include.rownames=FALSE) I can seem to get latex output which corresponds to the data.frame which is, \begin{table}[ht] \begin{center} \begin{tabular}{llll} \hline & & number of patients & \% \\ \hline Gender & & & \\ & Male & 3 & 0.428571428571429 \\ & Female & 4 & 0.571428571428571 \\ \hline \end{tabular} \end{center} \end{table}
It would be helpfull if you would state what exactly you did expect. As improvements you could reduce the digits, see ?xtable -------- Original Message -------- Subject: [R] "xtable" results doesn't correspond to data.frame From: Vumani Dlamini <dvumani at hotmail.com> To: r-help at stat.math.ethz.ch Date: 20.06.2007 11:20> Dear useRs, > Am trying to use xtable on the following data.frame and I don't get what I > expect: > > example.table <- data.frame(rbind( > c("Gender"," "," "," "), > cbind(rep(" ",2),c("Male","Female"),c(3.0,4.0),c(3/7,4/7)) > )) > colnames(example.table) <- c(" "," ","number of patients","%") > example.xtable <- xtable(example.table) > print.xtable(example.xtable, include.rownames=FALSE) > > I can seem to get latex output which corresponds to the data.frame which is, > \begin{table}[ht] > \begin{center} > \begin{tabular}{llll} > \hline > & & number of patients & \% \\ > \hline > Gender & & & \\ > & Male & 3 & 0.428571428571429 \\ > & Female & 4 & 0.571428571428571 \\ > \hline > \end{tabular} > \end{center} > \end{table} > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > > >
That table below is not the table from your first mail. However I would do something like: library(xtable) testdata<-data.frame(gender=c(rep("male",3),rep("female",4)) testtab<-as.data.frame(table(testdata)) pct<-function(x){ x/sum(x)*100 } testtab$percent<-pct(testtab$Freq) rownames(testtab)<-c(1,2) colnames(testtab)<-c("gender","Freq","%") #it looks like: testtab #and latex: xtable(testtab,digits=2) % latex table generated in R 2.5.0 by xtable 1.4-6 package % Wed Jun 20 18:25:22 2007 \begin{table}[ht] \begin{center} \begin{tabular}{rlrr} \hline & gender & Freq & \% \\ \hline 1 & female & 3 & 42.86 \\ 2 & male & 4 & 57.14 \\ \hline \end{tabular} \end{center} \end{table} I hope this is now how expected. Cheers Stefan -------- Original Message -------- Subject: Re:[R] "xtable" results doesn't correspond to data.frame From: Vumani Dlamini <dvumani at hotmail.com> To: singularitaet at gmx.net Date: Wed Jun 20 2007 13:22:59 GMT+0200> This is what is not expected. Having "Gender" repeated twice and the > labels for gender missing. Will format the digits and align once > everything is right. > Thanks. > > > \begin{table}[ht] > \begin{center} > \begin{tabular}{llrr} > \hline > & & number of patients & \% \\ > \hline > Gender & Gender & & \\ > & & 3 & 0.428571428571429 \\ > & & 4 & 0.571428571428571 \\ > \hline > \end{tabular} > \end{center} > \