r_dataframes Flashcards
1
Q
Create a data frame named “planets_df” with the following vectors:
- name,
- type,
- diameter,
- rotation,
- rings
A
planets_df <- data.frame(name, type, diameter, rotation, rings)
2
Q
Examine the structure of the “planets_df”
A
str(planets_df)
3
Q
For the data frame “planets_df”, print out the following:
- The diameter of Mercury: this is the value at the first row and the third column.
- From planets_df, select all data on Mars (the fourth row).
- The first 5 values in the “diameter” column of planets_df
A
- planets_df[1,3]
- planets_df[4,]
- planets_df[1:5, “diameter”]
4
Q
- Select the “rings” variable from “planets_df”. Store the vector that results as rings_vector.
- Select all columns for planets with rings
- Do this with the subset function
A
- rings_vector <- planet_df$rings
- planets_df[rings_vector,]
- subset(planet_df, subset = rings)
5
Q
Adapt the code so that instead of only the name column, all columns for planets that have rings are selected.
A
planets_df[rings_vector,]