How to format a table (data.frame) with pdf publishing quality (latex) in R?

Asked

Viewed 1,784 times

9

Suppose the following table:

tabela <- structure(list(Sexo = structure(c(1L, 1L, 2L, 2L), .Label = c("Homem", 
"Mulher"), class = "factor"), Grupo = structure(c(1L, 2L, 1L, 
2L), .Label = c("A", "B"), class = "factor"), Média = c(0.2655086631421, 
0.37212389963679, -0.835628612410047, 1.59528080213779), Var = c(0.329507771815361, 
0.820468384118015, 0.572853363351896, 0.908207789994776)), .Names = c("Sexo", 
"Grupo", "Média", "Var"), row.names = c(NA, -4L), class = "data.frame")

tabela
    Sexo Grupo      Média        Var
1  Homem     A  0.2655087  0.3295078
2  Homem     B  0.3721239  0.8204684
3 Mulher     A -0.8356286  0.5728534
4 Mulher     B  1.5952808  0.9082078

How to leave it in publication format, to a file on pdf (using latex for example)?

3 answers

9


You can also use the package stargazer

library(stargazer)
stargazer(tabela, summary=FALSE)

Upshot:

inserir a descrição da imagem aqui

8

A package that I think is fantastic is the tables. It is very flexible. For example, arranging the tabela in three different ways:

library(tables)
tabela1 <- tabular(~(Sexo)*Heading()*Grupo*Heading()*identity*(Média+Var), data=tabela)
tabela2 <- tabular((Sexo)~(Grupo)*Heading()*identity*(Média+Var), data=tabela)
tabela3 <- tabular(Sexo*Grupo~Heading()*identity*(Média+Var), data=tabela)

To generate the code latex:

latex(tabela1)
latex(tabela2)
latex(tabela3)

Results:

inserir a descrição da imagem aqui

5

You can use the package xtable. First, you need to download the package. In R, enter the following command:

install.packages("xtable")

Once installed, load it:

library("xtable")

Put your tabela in format data.frame and apply the command:

xtable(tabela)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.