Consider the following example:
tab <- table(mtcars$vs, mtcars$cyl, dnn = c("vs", "cylinder"))
prop.table(tab)
# cylinder
# vs 4 6 8
# 0 0.03125 0.09375 0.43750
# 1 0.31250 0.12500 0.00000
round(prop.table(tab)*100, 1)
# cylinder
# vs 4 6 8
# 0 3.1 9.4 43.8
# 1 31.2 12.5 0.0
Desired output:
# cylinder
# vs 4 6 8
# 0 3.1% 9.4% 43.8%
# 1 31.2% 12.5% 0.0%
scales::percent(round(prop.table(tab)))
does not work because there is no applicable method for plyr::round_any()
applied to an object of class table
.
I know I'm missing a simple workaround. Or perhaps a simple wrapper or pull request to plyr::round_any()
could fix this for everyone?
This should work.
c
being used here for its property of turning a table or matrix into a vector.Alternative using
sprintf
:If you want the table written without quotes, then you could use for example: