1. Learn
  2. /
  3. Courses
  4. /
  5. Helsinki Open Data Science

Connected

Exercise

Mutations

Mutating a data frame means adding new variables as mutations of the existing ones. The mutate() function is from the 'dplyr' package which is part of the 'tidyverse' packages. The tidyverse includes several packages that work well together, such as 'dplyr' and 'ggplot2'.

The tidyverse functions have a lot of similarities. For example, the first argument of the tidyverse functions is usually data. They also have other consistent features which makes them work well together and easy to use.

Instructions

100 XP
  • Mutate alc by creating the new column alc_use by averaging weekday and weekend alcohol consumption.
  • Draw a bar plot of alc_use.
  • Define a new asthetic element to the bar plot of alc_use by defining fill = sex. Draw the plot again.
  • Adjust the code: Mutate alc by creating a new column high_use, which is true if alc_use is greater than 2 and false otherwise.
  • Initialize a ggplot object with high_use on the x-axis and then draw a bar plot.
  • Add this element to the latter plot (using +): facet_wrap("sex").