Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Visualize your data - pie charts
9. Prepare the data – calculate the percentages
Check yourself

## Instruction

Ok! Now we know what chart we will create, so let's prepare the data.

In the previous chapter about distributions, we needed to prepare frequencies for bar charts. In pie charts, percentages play the main role. They will help us visualize the size of a particular part in comparison to the whole.

To calculate percentages for our categories' numerical column, we will use the mutate function. This function lets us create a new variable in our dataset. It works on each row or group in the dataset. This is its syntax:

dataset <- mutate(dataset, name_of_new_variable = formula_for_this_variable)

We need to calculate percentages, so the first argument (name_of_new_variable) will contain the new column name percent. The second (formula_for_this_variable) argument will contain this formula:

(variable) / sum(variable) * 100

This will find the variable value for each observation, divide it by the sum of all variable values, and multiply the result by 100. The result of this operation for this observation will be placed in new "percent" column.

## Exercise

Add a column that contains the percentages of total alcohol consumption to the france_beverages dataset - name the column percentage. Use the mutate function. Put france_beverages as the dataset and use the above percent formula on the consumption variable. Pass the result of this command to the france_beverages object

When you're done, press button.

### Stuck? Here's a hint!

You should write:

france_beverages <- mutate(france_beverages, percentage = consumption/sum(consumption) * 100)