3) Lists and Matrices
UC Berkeley, STAT 133, Fall 2024
π Lecture
This week in class we discuss more data objects in R, specifically matrices and lists.
Although we wonβt work with matrices that much in Stat 133, itβs worth learning the following:
- Why matrices, like vectors, are atomic objects,
- In what sense a matrix is a 2-dimensional array,
- How R stores matrices,
- Functions to create matrices, and inspect their structure
- How to manipulate matrices using bracket notation
As for lists, they are the most generic kind of data container in R, and youβll learn:
- In what sense a list is a non-atomic atomic
- In what sense a list is a one-dimensional object
- How to create lists
- Different ways to manipulate lists
π Reading
Read chapters 6, 7 and 8 of βR Coding Basicsβ:
π¬ Lab
In lab you will practice creating and manipulating matrices and lists.
π― Objectives
- Create simple matrices with
matrix()
- Describe and give examples of matrix subsetting (subscripting, indexing)
- Explain in what sense a matrix is a 2-dimensional object
- Describe and give examples of subsetting (subscripting, indexing) of an R list
- Provide an example of list subsetting with single brackets (e.g.
lis[index]
) - Provide an example of list subsetting with double brackets, and a single element (e.g.
lis[[index]]
) - Provide a manipulation example of a list with the dollar operator (e.g.
lis$wagon
)
π Shiny Friday
The shiny app of this week is based on the computation of Future Value (compounding interest) in its simplest version:
\[ FV = P (1 + r)^n \]
This is a simple app that takes three inputs:
- P = principal or initial amount (how much you deposit)
- r = annual interest rate (or rate of return)
- n = number of years
In turn, the app produces one (graphical) output which is a timeline to visualize the Future Value.
There are two versions of the app: one uses three sliders (one slider per input), and the other one uses three numeric inputs (one number per input)
π HW Assignments
- HW1 due this 9/13
- HW2 released on 9/14, due 9/20