Sandbox Tasks

Solve the following tasks using R. Each of the tasks are meant to be simple and can be solved combining the code we have seen in the first session.

  1. Load the example dataset mtcars which ships with R. Use data() to do so. Get an idea of the meaning of the variables in this dataset by reading the description.

  2. Which is the most economical car? Hint: Maximum miles per gallon (mpg)

  3. Display the first 3 rows of the mtcars data.frame

  4. Which is the most economical car with 8 cylinders? Hint: create a subset first and then proceed as in 2.

  5. How many cars with automatic gear are in the dataset? Hint: column am holds the information. Explore functions: table(), sum().

  6. Create a vector called a which contains 10 elements from 0 to 9

  7. Create a vector b that contains all elements of a except the 7th element. Which is number is missing compared to vector a?

ad 1.

data(mtcars)
`?`(mtcars)

ad 2.

# get the entire information
mtcars[which.max(mtcars$mpg), ]
##                 mpg cyl disp hp drat    wt qsec vs am gear carb
## Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.9  1  1    4    1
# only get the name, note that the name of the car is not a column, just a
# name !
row.names(mtcars[which.max(mtcars$mpg), ])
## [1] "Toyota Corolla"

ad 3.

Let's do this with the head() function. Indexing is possible as well of course.

head(mtcars, 3)
##                mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
mtcars[1:3, ]
##                mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

ad 4.

An easy-to-read-way is to subset first and then do the calculation like in 2.

cyl_8 <- subset(mtcars, cyl == 8)
cyl_8[which.max(cyl_8$mpg), ]
##                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Pontiac Firebird 19.2   8  400 175 3.08 3.845 17.05  0  0    3    2

We could even solve this very generally for any number of cylinders. This example was not discussed in the course so far and contains some more advanced concepts, like anonymous functions, apply() family and splitting by factors. Don't worry if you don't get it right away, it is just meant to illustrate the oportunities of programming - imagine there were hundreds of categories…



lapply(split(mtcars, factor(mtcars$cyl)), function(x) {
    x[which.max(x$mpg), ]
})
## $`4`
##                 mpg cyl disp hp drat    wt qsec vs am gear carb
## Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.9  1  1    4    1
## 
## $`6`
##                 mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## 
## $`8`
##                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Pontiac Firebird 19.2   8  400 175 3.08 3.845 17.05  0  0    3    2

ad 5.

# remember 1 = manual
table(mtcars$am)
## 
##  0  1 
## 19 13
# 0,1 encoding is favorable here
sum(mtcars$am)
## [1] 13

ad 6.

a <- 0:9

ad 7.

b <- a[-7]  # the answer is 6

# we can also find it using the computer
a[!(a %in% b)]
## [1] 6