dplyr - R calculating grouped frequency table with percentage -
this question has answer here:
giving following data.frame
, calculate occurance of each variable of var
, percentage of these occurence grouping variable group
:
group<-c("g1","g2","g1","g2","g3","g3","g1") var<-c("a","b","b","a","b","b","a") d<-data.frame(group,var)
with table()
, nice frequency table, counting occurences of combinations of 2 variables:
d<-as.data.frame(table(d)) group var freq 1 g1 2 2 g2 1 3 g3 0 4 g1 b 1 5 g2 b 1 6 g3 b 2
now calculate percentage of each variable var
group
. far i'm splitting data.frame group
, calculate percentage seperately g1
, g2
, g3
, merging afterwards.
d.g1<-d[d$group=="g1",] d.g1$per<-d.g1$freq/sum(d.g1$freq) d.g1 group var freq per 1 g1 2 0.6666667 4 g1 b 1 0.3333333
...
d.merge<-rbind(d.g1,d.g2,d.g3) d.merge group var freq per 1 g1 2 0.6666667 4 g1 b 1 0.3333333 2 g2 1 0.5000000 5 g2 b 1 0.5000000 3 g3 0 0.0000000 6 g3 b 2 1.0000000
is there more elegant solution using example reshape2
package?
with dplyr
package can do:
require(dplyr) d <- d %>% group_by(group) %>% mutate(per = freq/sum(freq))
Comments
Post a Comment