--5-Data frames
#1-What's a data frame?
#Click 'Submit Answer'.
#The data from the built-in example data frame mtcars
#will be printed to the console.
datasets::mtcars
# Print out built-in R data frame
mtcars#2-Quick, have a look at your data set
# Call head() on mtcars
head(mtcars)#3-Have a look at the structure
# Investigate the structure of mtcars
str(mtcars)
class(mtcars)
attributes(mtcars)#4-Creating a data frame
#Use the function data.frame() to construct a data frame.
#Pass the vectors name, type, diameter, rotation and rings
#as arguments to data.frame(), in this order.
# Definition of vectors
name <- c("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn",
"Uranus",
"Neptune")
type <- c("Terrestrial planet", "Terrestrial planet",
"Terrestrial planet",
"Terrestrial planet", "Gas giant", "Gas giant",
"Gas giant", "Gas giant")
diameter <- c(0.382, 0.949, 1, 0.532, 11.209, 9.449, 4.007, 3.883)
rotation <- c(58.64, -243.02, 1, 1.03, 0.41, 0.43, -0.72, 0.67)
rings <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)
# Create a data frame from the vectors
planets_df <-data.frame(name,type,diameter,rotation,rings)
planets_df#5-Creating a data frame (2)
#6-Selection of data frame elements
#7-Selection of data frame elements (2)
#8-Only planets with rings
#9-Only planets with rings (2)
#10-Only planets with rings but shorter
#11-Sorting
#12-Sorting your data frame
Last updated