i'm searching for an easy way to create a multivariat crosstable with two variables that only displays the mean, standard deviation and frequencies/samplesize of a third variable. In the best case with column sums and row sums.
Example with the mtcars dataset (table with "cyl" and "vs"; mean, standard deviation and frequencies of "hp"):
In Stata it would be tabulate cyl vs, summarize(hp) freq mean sta
.
To make clear how it ideally should look like I've made an example (the results are just made up):
or
Example 2 (results also just made up)
(Finally I have to transfer the table to LaTeX - ideally with stargazer - so it would be best of all if this were possible.)
I found a lot of solutions for crosstables with means, but only for two variables. And certainly not with mean, std. dev. and frequencies in one table. You would be a huge help to me.
edit: Now I tried this, but I don't know how to add standard deviation, column sums and row sums.
library(tidyr)
ct <- mtcars %>%
group_by(cyl, vs) %>%
summarise(hp = mean(hp, na.rm=TRUE), .groups = "drop") %>%
spread(vs, hp)
ct
ct %>%
adorn_rounding() %>%
adorn_ns(
ns = mtcars %>% # calculate the Ns on the fly by calling tabyl on the original data
tabyl(cyl, vs)
) %>%
adorn_title("combined", row_name = "Cylinders", col_name = "Is Automatic")
# stargazer(ct)
And I get this (means by group and frequencies in brackets):
Cylinders/Is Automatic 0 1
1 4 91 (1) 81.8 (10)
2 6 131.7 (3) 115.2 (4)
3 8 209.2 (14) NA (0)
edit2:
library(tidyr)
library(arsenal)
tab1 <- tableby(vs ~ hp, data=mtcars, strata = cyl, numeric.stats = c("meansd", "N"), test = FALSE)
summary(tab1, text = TRUE)
This sort of works for me, but to get the exact table - as in my illustration - into LaTeX, I unfortunately have to do a lot of work by hand.
Unfortunately stargazer(tab1)
does not work at all, because of
% Error: Unrecognized object type.
.
summary(tab1, text = "latex")
does work, but as mentioned to get a more or less beautiful, publication quality table in LaTeX there's a lot of work by hand.
Maybe it's too much to ask, but does anyone have any ideas?