简体   繁体   中英

R: Trying to represent a table using R Markup

I am trying to represent a table using R Markup, but it's simply not working. I am getting wrong results. My code is this:

```{r,echo=FALSE}
GenEmpA <- data.frame(matrix(table(DadosA$genero, useNA = "always")))
colnames(GenEmpA) <- "QA"
GenEmpA$percent <- c(round(GenEmpA$QA[1]/sum(GenEmpA)*100,digits=2),round(GenEmpA$QA[2]/sum(GenEmpA)*100,digits=2),round(GenEmpA$QA[3]/sum(GenEmpA)*100,digits=2))
GenEmpA$percent <- sub("$","%",GenEmpA$percent)
GenEmpB <- data.frame(matrix(table(DadosB$genero, useNA = "always")))
colnames(GenEmpB) <- "QB"
GenEmpB$percent <- c(round(GenEmpB$QB[1]/sum(GenEmpB)*100,digits=2),round(GenEmpB$QB[2]/sum(GenEmpB)*100,digits=2),round(GenEmpB$QB[3]/sum(GenEmpB)*100,digits=2))
GenEmpB$percent <- sub("$","%",GenEmpB$percent)
ResGenAB <- head(merge(x = GenEmpA,y = GenEmpB,by = NULL),n=3)
NovaLinha <- c(sum(GenEmpA$QA),"100%",sum(GenEmpB$QB),"100%")
ResGenAB = rbind(ResGenAB, NovaLinha)
rownames(ResGenAB) <- c("Masculino","Feminino","Não respondeu","Total")
colnames(ResGenAB) <- c("Frequência","Proporção","Frequência","Proporção")
ResGenAB
```

It's displaying this:

##               Frequência Proporção Frequência Proporção
## Masculino             31       50%         34    47.89%
## Feminino              29    46.77%         34    47.89%
## Não respondeu          2     3.23%         34    47.89%
## Total                 62      100%         71      100%

But I wanted it to show like this table: http://i.stack.imgur.com/GNUY0.jpg

My problem is to show the table without those "##" and looking like my image. Plus, on the fourth row in column 3, it's displaying 34, when it should be 3, don't know why.

This info might help to understand my data:

> GenEmpA
  QA percent
1 31     50%
2 29  46.77%
3  2   3.23%
> GenEmpB
  QB percent
1 34  47.89%
2 34  47.89%
3  3   4.23%

I am doing my Knit to Word.

Any help is gonna be very appreciated.

Instead of just using

ResGenAB

I have followed Stephane answer and used

kable(ResGenAB, format = "markdown")

It worked!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM