Indexing and selecting columns from a dataframe
In the same way as you indexed your vectors, you can select elements from your dataframe using square brackets. Different from dataframes however, you now have multiple dimensions: rows and columns. That's why you can use a comma in the middle of the brackets to differentiate between rows and columns. For instance, the following code planet_df[1,2]
would select the element in the first row and the second column from the dataframe planet_df
.
You can also use the $
operator to select an entire column from a dataframe. For instance, planet_df$planets
would select the entire planets column from the dataframe planet_df.
This exercise is part of the course
Basic Statistics
Exercise instructions
- Select the elements in the first row, and the second and third column from
planet_df
- Select the entire third column from
planet_df
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# select the values in the first row and second and third columns
# select the entire third column