Selection of data frame elements (2)

Instead of using numerics to select elements of a data frame, you can also use the variable names to select columns of a data frame.

Suppose you want to select the first three elements of the type column. One way to do this is

planets_df[1:3,2]

A possible disadvantage of this approach is that you have to know (or look up) the column number of type, which gets hard if you have a lot of variables. It is often easier to just make use of the variable name:

planets_df[1:3,"type"]

Instruction

Select and print out the first 5 values in the "diameter" column of planets_df.

load(url("http://s3.amazonaws.com/assets.datacamp.com/course/intro_to_r/planets.RData")) # The planets_df data frame from the previous exercise is pre-loaded # Select first 5 values of diameter column # The planets_df data frame from the previous exercise is pre-loaded # Select first 5 values of diameter column planets_df[1:5, "diameter"] msg = "Do not remove or overwrite the `planets_df` data frame!" test_object("planets_df", undefined_msg = msg, incorrect_msg = msg) test_output_contains("planets_df[1:5, \"diameter\"]", incorrect_msg = "Have you correctly selected the first five values from the diameter column and printed them out? You can use `[1:5, \"diameter\"]` here.") success_msg("Nice! Continue to the next exercise!")

You can select the first five values with planets_df[1:5, ...]. Can you fill in the ... bit to only select the "diameter" column?

Previous: 5-6 | Selection of data frame elements

Next: 5-8 | Only planets with rings

Back to Main