--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)
# Use str() to investigate the structure of the new planets_df variable.
# Check the structure of planets_df in no #4-Creating a data frame
str(planets_df)
#6-Selection of data frame elements
#From planets_df, select the diameter of Mercury:
#this is the value at the first row and the third column.
#Simply print out the result.
# 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
# Print out diameter of Mercury (row 1, column 3)
planets_df[1,3]
# Print out data for Mars (entire fourth row)
planets_df[4,]
#7-Selection of data frame elements (2)
#Select and print out the first 5 values in the "diameter"
#column of planets_df.
# 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
# Select first 5 values of diameter column
planets_df[1:5,"diameter"]
#8-Only planets with rings
#Use the $ sign to select the rings variable from planets_df.
#Store the vector that results as rings_vector.
#Print out rings_vector to see if you got it right.
# 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
# Select the rings variable from planets_df
rings_vector <- planets_df$rings
# Print out rings_vector
rings_vector
#9-Only planets with rings (2)
#The code on the right selects the name column of all planets
#that have rings.
#Adapt the code so that instead of only the name column,
#all columns for planets that have rings are selected.
# 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
# Select the rings variable from planets_df
rings_vector <- planets_df$rings
# Print out rings_vector
rings_vector
# Adapt the code to select all columns for planets with rings
planets_df[rings_vector,]
#10-Only planets with rings but shorter
#Use subset() on planets_df to select planets that have
#a diameter smaller than Earth.
#Because the diameter variable is a relative measure of
#the planet's diameter
#w.r.t that of planet Earth, your condition is diameter < 1.
# 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
# Select planets with diameter < 1
subset(planets_df,subset=diameter<1)
#11-Sorting
#Experiment with the order() function in the console.
#Click 'Submit Answer' when you are ready to continue.
# Play around with the order function in the console
cevi<-c(10,100,9)
order(cevi)
cevi[order(cevi)]
#12-Sorting your data frame
#Call order() on planets_df$diameter in no #4
#(the diameter column of planets_df). Store the result as positions.
#Now reshuffle planets_df with the positions vector as row indexes
#inside square brackets. Keep all columns. Simply print out the result.
# planets_df is pre-loaded in your workspace
# Use order() to create positions
# 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
positions <- order(planets_df$diameter)
# Use positions to sort planets_df
planets_df[positions,]
Last updated