STAT 385 Lab 01

Vector, Lists, Matrix, Array and Functions

Ha Khanh Nguyen

What You Need For This Lab

  • R and RStudio working on your personal computer.
  • Read and understand the following lecture notes:

Exercise 1: Rolling Dice

  • GOAL: an R function named roll_dice() that simulates rolling 6 dice and compute the average outcome of the dice.
    • Input: no input
    • Output: a number presenting the average of the 6 dice outcomes.
## [1] 3.666667
## [1] 2.833333

When you finish writing your function, copy the following code segment and run it in your RStudio console:

Exercise 2: Rolling Dice AGAIN

  • I know this is so boring, but we will get to the exciting stuff soon!
  • GOAL:
    • An R function that rolls a pair of dices.
    • One die is red and one die is green.
    • These dices are configurable (meaning there might be more or fewer than 6 faces).
    • Print out whether the outcome on the red die is greater than or equal to the out come on the green die.
  • Function should have:
    • Input:
      • red: value of the faces of the red die. If no value is provided, the default value is 1:6.
      • green: value of the faces of the green die. If no value is provided, the default value is 1:6.
    • Output:
      • TRUE if the outcome on the red die is greater than or equal to the out come on the green die.
      • FALSE otherwise.

Exercise 3: List of Lists

Here is the code to create the list used in this exercise:

## [[1]]
## [[1]][[1]]
## [1] "Ha"
## 
## [[1]][[2]]
## [1] 24
## 
## 
## [[2]]
## [[2]][[1]]
## [[2]][[1]][[1]]
## [1] "Alex"
## 
## [[2]][[1]][[2]]
## [1] 20
## 
## 
## [[2]][[2]]
## [[2]][[2]][[1]]
## [1] "Dave"
## 
## [[2]][[2]][[2]]
## [1] 21

Copy the above code and run it in your RStudio console.

Now, here are the tasks:

  • Get Alex’s age.
  • Get a list containing ONLY Dave’s name and age.
  • Get Ha’s name.

Exercise 4: Matrix

Here is the code to create the matrix used in this exercise:

##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12
## [4,]   13   14   15   16

Copy the above code and run it in your RStudio console.

Now, here are the tasks:

  • Get the element on the 1st row, 3rd column.
  • Get the element on the 3rd row, 2nd column.
  • Get all the elements from the 1st row.
  • Get all the elements from the 3rd column.
  • Compute the sum of all the elements from the 1st row.
  • Compute the average of all the elements from the 3rd column.