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.
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)
The final result is similar to the expected output.You can control the number of digits in the table, with the digits
function.