1

I have data in R and I'd like to create a nice frequency tables of each variable.

For example, variable Male with values 0 and 1 and how many of them there are. (After that, ideally to rename the rows.) And then export it to latex. I found nice function prop.table , however I am not able to switch rows and columns and export it to latex.

I know that stargazer which I use for regression output is also able to do this, but there are also Max Min Stan dev, I don't want to have in the table. What would you recommend to me?

There is an illustrative "table" how I would like to have the output.

Table Male


Item Number Per Cent


0
1
Total

And finally also give all variables into one table. Instead of 0 and 1 would be names of variables.

lmo
  • 37,904
  • 9
  • 56
  • 69
Lenka Tom
  • 13
  • 5
  • As I understand you would like to convert a table from R to latex. Is that correct? What about using `lstlisting`. –  Apr 08 '18 at 12:06
  • You do not provide enough information (or a reproducible example) to give a complete answer. However, you should check out the `xtable` package. It will output latex tables and can take various tabular objects as inputs. My recommendation is that you should calculate your desired results, convert them into a matrix, add your desired rownames and colnames, and then use `xtable` to get the latex result. – lmo Apr 08 '18 at 12:56
  • Probably check out the link to the right: [tools for making latex tables](https://stackoverflow.com/questions/5465314/tools-for-making-latex-tables-in-r?rq=1) for additional info. – lmo Apr 08 '18 at 13:00

1 Answers1

0

Suppose I have two variable male and female, and I want to know if they bought soup or not, 1 if I buy, 0 if I do not buy. I create a data.frame df, then I use the prop.table function that you suggest, finally I change the values of 0 and 1 in rownames.

library(xtable)
set.seed(2)
df = data.frame(Male = rbinom(10,1,0.6), Female = rbinom(10,1,0.4))
df
## Male Female
## 1 1 0
## 2 0 0
## 3 1 1
## 4 1 0
## 5 0 0
## 6 0 1
## 7 1 1
## 8 0 0
## 9 1 0
## 10 1 0

library(xtable)
tab = rbind(t(t(prop.table(table(df$Male))*100)),sum(prop.table(table(df$Male))*100))
rownames(tab) <- c("No Soup","Soup","Total")
colnames(tab) <- "Item Number Per Cent"
t1 = xtable(tab,caption = "Male", digits = 0)
print(t1, caption.placement = "top", comment = FALSE)

enter image description here

The final result is similar to the expected output.You can control the number of digits in the table, with the digits function.

Rafael Díaz
  • 2,134
  • 2
  • 16
  • 32