R for Everyone
  1. Files, Subsetting, & Tips
  2. 11  Handy exercises
  • Start
    • 1  Introduction
  • Foundations & Objects
    • 2  Foundations of R
    • 3  Foundations of R exercises
    • 4  R objects
    • 5  R objects exercises
    • 6  R objects solutions
  • Files, Subsetting, & Tips
    • 7  Files and subsetting data
    • 8  Files exercises
    • 9  Files solutions
    • 10  Handy tips & tricks
    • 11  Handy exercises
    • 12  Handy Solutions
  • Plots
    • 13  Histograms and Line graphs
    • 14  Scatter plots and Box plot
    • 15  Plots exercises
    • 16  Plots solutions
  • Stats & Programming
    • 17  Basic stats
    • 18  Stats exercises
    • 19  Stats solutions
    • 20  Basic programming
    • 21  Student grades example
  • Appendices
    • A  Further resources

In this chapter

  • 11.1 Tea exercise
  • 11.2 English speakers across the world exercise
  • 11.3 Extra exercise

11  Handy exercises

There has been a lot covered this section so these exercises will hopefully be straightforward.

Please set the working directory to your main workshop directory and use your “Exercises.R” script. Ensure you are using annotations and code sections to keep the contents clear and separated.

Additionally read in and write out files to “Chapter_10-11” .

11.1 Tea exercise

The first task you will carry out is printing out information from “tea_df”. Below is an example statement for the country Turkey:

“Turkey is the number 1 consumer of tea. It consumes 5.8kg of tea annually per capita.”

Print out this statement for the countries Ireland, United Kingdom, France, and Australia with their relevant information. Make sure the kilogram value only has one decimal place.

Tip: You will require the functions paste() and round() from day 1.

11.2 English speakers across the world exercise

The last exercise is to create the following table as a data frame called “english_100mil_df”. Use the “english_complete_datasets_df” data frame as a start.

Eligible population Total English speakers As first language As an additional language Fraction of population that are English speakers
United States 296603003 283160411 234171556 48988855 0.9546782
Nigeria 156493000 79000000 0 79000000 0.5048149
Philippines 110000000 64025890 36935 63988955 0.5820535
Bangladesh 163323100 30108031 709873 29398158 0.1843464
China 1210000000 10000000 0 10000000 0.0082645
Brazil 205000000 10542000 292000 10250000 0.0514244
Mexico 120664000 15686262 0 15686262 0.1299995
Mean 323154729 70360371 33601481 36758890 0.3450831
Total 2262083103 492522594 235210364 257312230 0.2177297

The data frame only contains countries that have an eligible population that is greater than 100 million (100000000). Ensure the “Total” row was not calculated using the “Mean row”.

When you have created yours check it with the above one. Is your value for the “Total” “Fraction of population that are English speakers” correct?.

Once you have created the data frame write it out as a comma separated file with the function write.table() called “English_top_7_populated_countries.csv”. Have the row and column names surrounded by quotes in your file. Make sure there is an empty value above your row names.

11.3 Extra exercise

If you still have time this session and you do not have any questions please attempt the following task:

Create a multiplication table like the one in the practice from today. However have the row and column names equal one to twelve.

Then write the data frame to a file. The name and format of the file is up to you.

There is no solution to this in the next section.

10  Handy tips & tricks
12  Handy Solutions