Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Know your problem
Know your data
Visualise your data
10. Step 3 - prepare your data
Work with your chart
Check yourself


A bar chart needs some numerical values for each group. In our problem, we will use the frequency of each category as the numerical values.

To find out these frequency values, we have to create a frequency table for the categorical variable (in this case, the pattern variable). Our frequency table looks like this:

pattern n
least risky 13
medium risky 109
most risky 2
somewhat risky 28
very risky 9

The first column (pattern) presents every possible or considered value of the pattern variable. The second column (n) shows how many times a value occurs in the dataset – in our case, this is the number of countries classified by each category.

We will need this frequency table to create a bar chart, so now you'll build one for yourself. To do that in the R programming language, you need to use the count function, which looks like this:

count(dataset, variable)

It takes two arguments: the dataset name and the name of the variable we are using for our frequency table categorical data.


Do a bit of programming and create a table called tab!

  1. Call the count() function using the alcohol_consumption dataset and the pattern variable.
  2. Assign the result of your code to the tab variable using the <- operator.
  3. Display the result by typing tab in the next line.
When you're done, press the Run and Check Code button to check your code.

Stuck? Here's a hint!

You should write:

tab <- count(alcohol_consumption, pattern)