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?
Several packages in R could be helpful. The one I have used is arsenal
which may be close to Stata tabulate, summarize
library(tidyr)
library(arsenal)
tab1 <- tableby(vs ~ hp, data=mtcars, strata = cyl,
numeric.stats = c("meansd", "N"), test = FALSE)
summary(tab1, text = TRUE)
#>
#>
#> |cyl | | 0 (N=18) | 1 (N=14) | Total (N=32) |
#> |:---|:------------|:----------------:|:---------------:|:----------------:|
#> |4 |hp | | | |
#> | |- Mean (SD) | 91.000 (NA) | 81.800 (21.872) | 82.636 (20.935) |
#> | |- N | 1 | 10 | 11 |
#> |6 |hp | | | |
#> | |- Mean (SD) | 131.667 (37.528) | 115.250 (9.179) | 122.286 (24.260) |
#> | |- N | 3 | 4 | 7 |
#> |8 |hp | | | |
#> | |- Mean (SD) | 209.214 (50.977) | NA | 209.214 (50.977) |
#> | |- N | 14 | 0 | 14 |
Created on 2021-03-15 by the reprex package (v1.0.0)
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.