I have a variable which can be a, b or c from different persons (A, B, C) and different years (1, 2, 3). It looks like this:
I want to calculate the percentages per colour field. For example: the three yellow rows together are 100%. A-2-a is than 13%, A-2-b is 48% and A-2-c is 39%. And the three green rows are together 100% as well.
I tried the follow, but it does not work. Can anybody help me?
group_by(Person, Year, Variable) %>%
dplyr::mutate(Count = n()) %>%
group_by(Person, Year) %>%
dplyr::mutate(Percentage = formattable::percent(count / sum(count), digits=1)) %>%
dplyr::select(Year, Variable, Count, Percentage) %>%
distinct()