Only planets with rings but shorter

So what exactly did you learn in the previous exercises? You selected a subset from a data frame (planets_df) based on whether or not a certain condition was true (rings or no rings), and you managed to pull out all relevant data. Pretty awesome! By now, NASA is probably already flirting with your CV ;-).

Now, let us move up one level and use the function subset(). You should see the subset() function as a short-cut to do exactly the same as what you did in the previous exercises.

subset(my_df, subset = some_condition)

The first argument of subset() specifies the data set for which you want a subset. By adding the second argument, you give R the necessary information and conditions to select the correct subset.

The code below will give the exact same result as you got in the previous exercise, but this time, you didn't need the rings_vector!

subset(planets_df, subset = rings)

Instruction

Use subset() on planets_df to select planets that have a diameter smaller than Earth. Because the diameter variable is a relative measure of the planet's diameter w.r.t that of planet Earth, your condition is diameter < 1.

load(url("http://s3.amazonaws.com/assets.datacamp.com/course/intro_to_r/planets.RData")) # planets_df is pre-loaded in your workspace # Select planets with diameter < 1 # planets_df is pre-loaded in your workspace # Select planets with diameter < 1 subset(planets_df, subset = diameter < 1) msg = "Do not remove or overwrite the `planets_df` data frame!" test_object("planets_df", undefined_msg = msg, incorrect_msg = msg) test_correct({ test_output_contains("subset(planets_df, subset = diameter < 1)", incorrect_msg = "Have you correctly specified the `subset = ...` part inside `subset()`. The condition in this case is `diameter < 1`. Simply print out the result.") }, { test_function("subset", args = "x", not_called_msg = "Make sure to use the `subset()` function to perform the selection!", incorrect_msg = "The first argument you pass to `subset()` should be `planets_df`.") }) success_msg("Great! Not only is the `subset()` function more concise, it is probably also more understandable for people who read your code. Continue to the next exercise.");

subset(planets_df, subset = ...) almost solves it; can you fill in the ...?

Previous: 5-9 | Only planets with rings (2)

Next: 5-11 | Sorting

Back to Main