title : Insert the chapter title here description : Insert the chapter description here attachments : slides_link : https://s3.amazonaws.com/assets.datacamp.com/course/teach/slides_example.pdf
--- type:VideoExercise lang:r xp:50 skills:1
*** =video_link //player.vimeo.com/video/154783078
--- type:MultipleChoiceExercise lang:r xp:50 skills:1
Have a look at the plot that showed up in the viewer to the right. Which type of movie has the worst rating assigned to it?
*** =instructions
- Adventure
- Action
- Animation
- Comedy
*** =hint Have a look at the plot. Which color does the point with the lowest rating have?
*** =pre_exercise_code
# The pre exercise code runs code to initialize the user's workspace. You can use it for several things:
# 1. Preload a dataset. The code below will read the csv that is stored at the URL's location.
# The movies variable will be available in the user's console.
movies <- read.csv("http://s3.amazonaws.com/assets.datacamp.com/course/introduction_to_r/movies.csv")
# 2. Pre-load packages, so that users don't have to do this manually.
library(ggplot2)
# 3. Create a plot in the viewer, that students can check out while reading the exercise
ggplot(movies, aes(x = runtime, y = rating, col = genre)) + geom_point()
*** =sct
# The sct section defines the Submission Correctness Tests (SCTs) used to
# evaluate the student's response. All functions used here are defined in the
# testwhat R package
msg_bad <- "That is not correct!"
msg_success <- "Exactly! There seems to be a very bad action movie in the dataset."
# Use test_mc() to grade multiple choice exercises.
# Pass the correct option (Action, option 2 in the instructions) to correct.
# Pass the feedback messages, both positive and negative, to feedback_msgs in the appropriate order.
test_mc(correct = 2, feedback_msgs = c(msg_bad, msg_success, msg_bad, msg_bad))
--- type:NormalExercise lang:r xp:100 skills:1
In the previous exercise, you saw a dataset about movies. In this exercise, we'll have a look at yet another dataset about movies!
A dataset with a selection of movies, movie_selection
, is available in the workspace.
*** =instructions
- Check out the structure of
movie_selection
. - Select movies with a rating of 5 or higher. Assign the result to
good_movies
. - Use
plot()
to plotgood_movies$Run
on the x-axis,good_movies$Rating
on the y-axis and setcol
togood_movies$Genre
.
*** =hint
- Use
str()
for the first instruction. - For the second instruction, you should use
...[movie_selection$Rating >= 5, ]
. - For the plot, use
plot(x = ..., y = ..., col = ...)
.
*** =pre_exercise_code
# Pre-load a package in the workspace
library(MindOnStats)
# You can prepare the data before the student starts:
data(Movies)
movie_selection <- Movies[Movies$Genre %in% c("action", "animated", "comedy"),c("Genre", "Rating", "Run")]
# You can also clean up data so that it's not available in the student's workspace anymore:
rm(Movies)
*** =sample_code
# movie_selection is available in your workspace
# Check out the structure of movie_selection
# Select movies that have a rating of 5 or higher: good_movies
# Plot Run (i.e. run time) on the x axis, Rating on the y axis, and set the color using Genre
*** =solution
# movie_selection is available in your workspace
# Check out the structure of movie_selection
str(movie_selection)
# Select movies that have a rating of 5 or higher: good_movies
good_movies <- movie_selection[movie_selection$Rating >= 5, ]
# Plot Run (i.e. run time) on the x axis, Rating on the y axis, and set the color using Genre
plot(good_movies$Run, good_movies$Rating, col = good_movies$Genre)
*** =sct
# The sct section defines the Submission Correctness Tests (SCTs) used to
# evaluate the student's response. All functions used here are defined in the
# testwhat R package. Documentation can also be found at github.com/datacamp/testwhat/wiki
# Test whether the function str is called with the correct argument, object
# If it is not called, print something informative
# If it is called, but called incorrectly, print something else
test_function("str", args = "object",
not_called_msg = "You didn't call `str()`!",
incorrect_msg = "You didn't call `str(object = ...)` with the correct argument, `object`.")
# Test the object, good_movies
# Notice that we didn't define any feedback here, this will cause automatically
# generated feedback to be given to the student in case of an incorrect submission
test_object("good_movies")
# Test whether the student correctly used plot()
# Again, we use the automatically generated feedback here
test_function("plot", args = "x")
test_function("plot", args = "y")
test_function("plot", args = "col")
# Alternativeley, you can use test_function() like this
# test_function("plot", args = c("x", "y", "col"))
# It's always smart to include the following line of code at the end of your SCTs
# It will check whether executing the student's code resulted in an error,
# and if so, will cause the exercise to fail
test_error()
# Final message the student will see upon completing the exercise
success_msg("Good work!")