# --5-Data frames

\#1-What's a data frame?

```r
#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

```r
# Call head() on mtcars
head(mtcars)
```

\#3-Have a look at the structure

```r
# Investigate the structure of mtcars
str(mtcars)
class(mtcars)
attributes(mtcars)
```

\#4-Creating a data frame

```r
#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)

```r
# 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

```r
#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)

```r
#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

```r
#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)

```r
#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

```r
#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

```r
#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

```r
#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,]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://r-pedia.gitbook.io/cevi/r-programming-track/a-introduction-to-r/5-data-frames.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
