I have a dataset in which four variables have the same values. Now I want to calculate the percentage shares for the values in each variable, so I can plot them in a stacked bar chart.
This is an example dataset:
climate_change air_quality water_polution trash
<chr> <chr> <chr> <chr>
1 Not a very serious probl~ A somewhat serious prob~ A somewhat serious problem A very serious problem
2 Not a very serious probl~ Not a very serious prob~ Not a very serious problem Not a very serious pr~
3 NA NA NA NA
4 NA NA NA NA
5 A very serious problem A very serious problem A very serious problem A very serious problem
6 A somewhat serious probl~ A very serious problem Not at all a serious prob~ A somewhat serious pr~
I know how to calculate the percentage shares for each variable, e.g.:
lebanon %>%
filter(!is.na(climate_change)) %>%
count(climate_change) %>%
mutate(prop = n / sum(n))
Getting:
climate_change n prop
<chr> <int> <dbl>
1 A somewhat serious problem 348 0.286
2 A very serious problem 620 0.510
3 Not a very serious problem 202 0.166
4 Not at all a serious problem 45 0.0370
What I want now is to find a solution for keeping the values listed as rows while having the variables listed as columns with n and/or prop values. What would be the most efficient way to get that?
I want to have something like that:
climate_change air_quality ..... .....
<chr> <dbl>
1 A somewhat serious problem 0.286 .....
2 A very serious problem 0.510 .....
3 Not a very serious problem 0.166
4 Not at all a serious problem 0.0370
I had a hard time desciribing this problem and to find a similiar question on this site. I hope I have described it well and in the case you know a similiar question, just link it here. :)
Greetings