πŸ“Œ Solutions for example tasks

This is where you’ll find solutions for all of the tutorials (mostly after we have discussed them in the seminar).

Solutions for Tutorial 3

Task 3.1

Create a subfolder called β€œdata” in your current working environment. Download the text file β€œdata_halloween.txt” (via Moodle/Data for R). Save the text file in the subfolder and try to load it into R as an object called data_halloween.

Solution:

Task 3.2

In this subfolder called β€œdata”, try to write out the file you just created called data_halloween as a .csv file. You may have to google for the right command.

Solution:

Solutions for Tutorial 4

Task 4.1

Create a data frame called data. The data frame should contain the following variables (in this order):

  • a vector called food. It should contain 5 elements, namely the names of your five favourite dishes.
  • a vector called description. For every dish mentioned in food, please describe the dish in a single sentence (for instance, if the first food you describe is β€œpizza”, you could write: β€œThis is an Italian dish, which I prefer with a lot of cheese.”)
  • a vector called rating. Rate every dish mentioned in food with 1-5 (using every number only once), i.e., by rating your absolute favorite dish out of all five with a 1 and your least favorite dish out of all five with a 5.

Solution:

Task 4.2

Can you sort the data in your data set by rating - with your favorite dish (i.e., the one rated β€œ1”) on top of the list and your least favourite dish (i.e., the one rated β€œ5”) on the bottom?

Important: You do not yet know this command - you’ll have to google for the right solution. Please do and note down the exact search terms you used for googling, so we can discuss them next week.

Solution:

Solutions for Tutorial 5

Task 5.1

Read the data set into R. Writing the corresponding R code, find out

  • how many observations and how many variables the data set contains.

Solution:

Task 5.2

Writing the corresponding R code, find out

  • how many candy bars contain chocolate.
  • how many candy bars contain fruit flavor.

Solution:

Task 5.3

Writing the corresponding R code, find out

  • the name(s) of candy bars containing both chocolate and fruit flavor.

Solution:

Task 5.4

Create a new data frame called data_new. Writing the corresponding R code,

  • reduce the data set only to observations containing chocolate but not caramel. The data set should also only include the variables competitorname and pricepercent.
  • round the variable pricepercent to two decimals.
  • sort the data by pricepercent in descending order, i.e., make sure that candy bars with the highest price are on top of the data frame and those with the lowest price on the bottom.

Solution:

Solutions for Tutorial 7