I am struggling with using the crosstab
function, available at the link:
source("http://pcwww.liv.ac.uk/~william/R/crosstab.r")
I have also used the gtsummary
library and converted mtcars
to a tibble, then converted the vs
and am
columns to characters using:
library(gtsummary)
mtcars = mtcars %>% as_tibble()
mtcars = apply(mtcars %>% select(vs, am), 2, as.character)
crosstab(mtcars, row.vars = "vs")
crosstab(mtcars, row.vars = "am")
I would like to achieve the same using the apply
or sapply
function, without using the crosstab
function's row.vars
argument, which I do not know how to set.
I have tried
apply(mtcars, 2, crosstab(mtcars, row.vars = c('vs', 'am')))
But it does not work. Is there an alternative way to achieve this, even with the sapply
function?
Thanks.