Reputation: 53
let's say I have this data set :
footballplayers Nationality
A Germany
B Germany
C France
D France
E Belgium
F Belgium
I took the count :
df %>% group_by(Nationality) %>% summarise(count=n())
Nationality count
Germany 2
France 2
Belgium 2
now I have to find the percentage of each nationality: 2/6 *100 for example in this case. so how to do it in a single query after taking the count? so that I can use it in the pie chart.
Upvotes: 0
Views: 42
Reputation: 429
Try this
df %>%
group_by(Nationality) %>%
summarise(Count = n()) %>%
mutate(percentage = round(Count / sum(Count) * 100, 2))
You can omit the round function if you do not want to round the percentage!
Upvotes: 2