2) R Vectors

đź“– Lecture

This week we describe data types and their implementation as vectors in R (the most fundamental data object in R). As you’ll see, R is—to a large extent—a vector-based language. Pretty much all other data objects are derived from vectors. We’ll focus on important concepts like:

  • main data types (“atomic types” in R): logical, integer, real, and character

  • creation of vectors

  • implicit coercion rules: what R does when you combine values of different data types

  • vectorization: when R applies calculations or operations to all the elements of a vector (element-wise)

  • recycling: what R does when you perform a calculation with vectors of different length

  • subsetting (aka subscripting, subindexing, indexing): the use of single/double brackets to subset (i.e. subscript, index) elements of a vector

đź“š Reading

Read the chapters that form Part I of “R Coding Basics”:

🔬 Lab

You’ll practice creating and manipulating vectors in R, and learning about the concepts described above.

🎯 Objectives

At the end of this week you will be able to:

  • Describe the four common data types in R, and give examples for them
  • Explain why R vectors are said to be atomic objects
  • Describe and give an example of the implicit coercion rules
  • Describe and give an example of vectorized code
  • Describe and give an example of the recycling rule
  • Describe and give an example of subsetting (indexing) vectors

🔆 Shiny Friday

Shiny is an open source R package (developed by Posit) that lets you build web applications using R (and RStudio). BTW: If you’ve never seen a shiny app, you can find dozens of examples in a gallery curated by Posit: https://shiny.posit.co/r/gallery/

Every Friday lecture (starting on Jan-26) I will use a shiny example, describing various aspects about its structure, and reviewing some of its key details. Every week we’ll go over a different shiny app, increasing the level of complexity, but also making it more interactive and richer.

Our first shiny app involves the app that comes by default in RStudio called Old Faithful. This app is based on the famous geyser Old Faithful located in Yellowstone National Park. In particular, the app uses R’s built-in data set "faithful" which contains two variables:

  1. eruptions: is the time (in min) of an eruption
  2. waiting: is the waiting time (in min) to next eruption

The app is a very basic app that displays a histogram of the “waiting times”.

https://github.com/data133/shiny/tree/main/old-faithful1-default

đź”” Activities

HW1 instructions will be released at the end of this week (due 02/02).