Apply function table() to each column of a data.frame using dplyr
I often apply the table-function on each column of a data frame using plyr, like this:
library(plyr)
ldply( mtcars, function(x) data.frame( table(x), prop.table( table(x) ) ) )
Is it possible to do this in dplyr also?
My attempts fail:
mtcars %>% do( table %>% data.frame() )
melt( mtcars ) %>% do( table %>% data.frame() )
long
form usinggather
fromlibrary(tidyr)
and then dogather(mtcars, Var, Val) %>% group_by(Var) %>% dplyr::mutate(n=n()) %>% group_by(Var,Val) %>% dplyr::mutate(n1=n(), Percent=n1/n)%>% unique()
– Gareth