Using dplyr
I'm generating a simple summary table for two categories:
# Data
data("mtcars")
# Lib
require(dplyr)
# Summary
mt_sum <- mtcars %>%
group_by(am, gear) %>%
summarise(n = n()) %>%
spread(key = am, value = n)
Which produces the desired results:
Source: local data frame [3 x 3]
gear 0 1
(dbl) (int) (int)
1 3 15 NA
2 4 4 8
3 5 NA 5
To the generated table I would like to add a set of columns that would have row percentages instead of the presently available totals.
Desired results
I would like for my table to look like that:
gear 0 1 0per 1per
1 3 15 NA 100%
2 4 4 8 33% 67%
3 5 NA 5 100%
Attempts
I tried to achieve the following by adding the code:
mt_sum <- mtcars %>%
group_by(am, gear) %>%
summarise(n = n()) %>%
spread(key = am, value = n) %>%
mutate_each(funs(./rowSums(.)))
but it returns the following error:
Error: 'x' must be an array of at least two dimensions
Hence my question: how can I add extra columns with row percentage values in dplyr
?
Side points
- I would prefer blank values instead of
NAs
- The table could be easily build with use of
CrossTable
ingmodels
but I would like to stay indplyr
as I want to keep as many transformations as possible in one place