You will often want to select an entire column, namely one specific variable from a data frame. If you want to select all elements of the variable diameter
, for example, both of these will do the trick:
planets_df[,3]
planets_df[,"diameter"]
However, there is a short-cut. If your columns have names, you can use the $
sign:
planets_df$diameter
$
sign to select the rings
variable from planets_df
. Store the vector that results as rings_vector
.rings_vector
to see if you got it right.
load(url("http://s3.amazonaws.com/assets.datacamp.com/course/intro_to_r/planets.RData"))
# planets_df is pre-loaded in your workspace
# Select the rings variable from planets_df
rings_vector <-
# Print out rings_vector
# planets_df is pre-loaded in your workspace
# Select the rings variable from planets_df
rings_vector <- planets_df$rings
# Print out rings_vector
rings_vector
msg = "Do not remove or overwrite the `planets_df` data frame!"
test_object("planets_df", undefined_msg = msg, incorrect_msg = msg)
test_object("rings_vector",
incorrect_msg = "Have you correctly selected the `rings` variable from `planets_df`? Use `$rings`. Store the result as `rings_vector`.")
test_output_contains("rings_vector", incorrect_msg = "Don't forget to print out `rings_vector` after you've created it!")
success_msg("Great! Continue to the next exercise and discover yet another way of subsetting!")
planets_df$diameter
selects the diameter
column from planets_df
; what do you need to select the rings
column then?