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 "