Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Know your data
Visualize your data – categorical variables
Work with your chart 2
Check yourself 2

Instruction

We know what to expect from the chart. Now let's prepare the data to put on it.

The first stage of this process will generate data for the width of the rectangles. In this case, we will need percentages for each wealth category.

To do that, we will count how often each category of wealth occurs and transform that information to a percentage. Firstly, we will use the count() function, that we already know, on our dataset to calculate frequencies. Here is how this will look:

tab <- count(dataset, variable_name)

And the new tab table will look like this:

 variable_name   n 
1         cat1   30 
2         cat2   23

Now we can create a new variable that corresponds to these percentages. We do this by adding the mutate function, using these calculated frequencies:

tab <-  mutate(tab, percent = n / sum(n) * 100)

Exercise

Find how often each category of the wealth_index_cat variable occurs and show it as a percentage of the whole. Use the two functions described above, but make sure to put the dataset name and the variable in the proper places. The dataset name is alcohol_wealth2.

When you're done, press the Run and Check Code button.

Stuck? Here's a hint!

You should write:

tab <- count(alcohol_wealth2, wealth_index_cat)
tab <- mutate(tab,percent = n/sum(n) * 100)