I have a data.frame of categorical variables that I have divided into groups and I got the counts for each group.
My original data nyD looks like:
Source: local
You can do this whole thing in one step (from your original data nyD
and without creating ny1
). That is because when you'll run mutate
after summarise
, dplyr
will drop one aggregation level (v2
) by default (certainly my favorite feature in dplyr
) and will aggregate only by v1
nyD %>%
group_by(v1, v2) %>%
summarise(count = n()) %>%
mutate(prop = count/sum(count))
# Source: local data frame [5 x 4]
# Groups: v1
#
# v1 v2 count prop
# 1 a minus 1 0.3333333
# 2 a plus 2 0.6666667
# 3 b minus 1 0.5000000
# 4 b x 1 0.5000000
# 5 c x 2 1.0000000
Or a shorter version using count
(Thanks to @beginneR)
df %>%
count(v1, v2) %>%
mutate(prop = n/sum(n))