Vector Objects and Functions

Course logistics and announcements

  • Assignment: Homework 1 due today

  • Assignment: Homework 2 due next week

  • Submission: Complete in Posit or Posit Cloud, submit for grading through Canvas

  • Due time: 10am Fridays (before morning session)

  • Support: Attend student drop-in hours with Herong and Troy for help getting started with R and Posit on your personal computer

  • Group work: Update small group membership as needed (join your nearest group)

  • Session structure: Morning section and Afternoon section

Outline of today

  • Outline: Review • Slides: Vector objects • Break • Coding together: Vector objects

  • Next time: Functions & data frame objects

Learning objectives: Vector objects

  • Classify one dimensional object types in R

  • Distinguish between values and positions of vector objects

  • Build numeric and character vectors

Objects in R (key concepts)

  • Objects are anything you assign a name to in R

  • R is an object-oriented language

  • Objects are classified based on the type of information you put in them

    • Examples: Variables, Datasets, Graphs, Results, Functions

  • Objects are temporary

Assignment in R (syntax and semantics)

  • Assignment happens with the operator <-; code is read from right to left across the assignment operator

    • Example: pajama s
      <- 5

    • R reads this as: assign the value 5 to an object called “pajamas”

    • Query: What is pajamas? > pajamas
      [1] 5

    • Square brackets [] show positions

  • In PH500, assignment can also happen with =

Naming objects: rules to avoid errors

  • Object names cannot contain operator symbols like !, +, -, #

  • No spaces in object names

  • A dot (.) and an underscore (_) are allowed; a name can start with a dot

  • Object names can contain a number but cannot start with a number

  • R is case sensitive (e.g., X vs x are different objects)

  • TIP: Don’t name an object after a commonly used function (e.g., mean)

R data object types (classes)

  • Vectors - usually a column, a single type of data

  • Matrices

  • Data frames - data sets

  • Lists - groups of different objects

  • S4

Vector objects (definition and properties)

  • One-dimensional, ordered set of values (one-dimension)

  • Order matters

  • Each element has a value and a position

  • Can have any number of elements, but all must be the same type

  • Example vector values (numeric): 6.2,\, 9.3,\, 2.1,\, 1.4,\, 5.5,\, 9.8

    • Vector positions: [1, 2, 3, 4, 5, 6]

    • Element at position 4 is the value 1.4

  • Typical construction (numeric): a vector like c(6.2, 9.3, 2.1, 1.4, 5.5, 9.8)

Types of vectors and examples

  • Numeric

    • Integer: 7, 10, 35, 100 - whole numbers that are positive

    • Double: 3.1, -0.7, 22.4, -45.9 - decimals, can be negative

  • Character: "UM", "MSU", "UConn", "Maryland"

  • Factor (categorical with levels): Levels: low, medium, high (example levels shown: high, low, low, medium)

  • Logical: TRUE, FALSE, FALSE, TRUE (also shown as T, F, F, T)

  • Date: \text{2011-02-27} style strings like "2021-10-27", "2021-11-3", "2021-11-10", "2021-11-17"

Indexing vectors (subsetting)

  • Use [] to index by position (select on positions)

    • Example: select value of object named cats in position 4: cats[4]

  • Select by value criteria (logical indexing):

    • Example: select positions where values are less than 5: cats[cats < 5]

  • Note on evaluation order: When there are () or [], R reads code from the inside out

Recap: Vector objects (summary)

  • All objects have a name, a class/type, value(s), and position(s)

  • Assign objects with <-

  • Objects have a class based on their contents

  • Vectors are a one-dimensional class of objects

  • Vector types include numeric, character, factor, logical

  • Objects contain values with positions

  • Index objects with []

Short break

  • Take a 5-minute break! Restart at X:XX for the second half of the lecture

Learning objectives: Functions

  • Implement functions to perform actions on data

  • Install and load packages to access functions

Functions: what they do

  • Actions in R, how to perform tasks

  • Functions accept input object and provide transformed output

  • Examples: mean(), min(), max()

Basic structure of function coding

  • Structure: > function(object, options)

  • The results will be printed in the console for viewing

  • Or you can store the output of a function as a new object to use later: output <- function(object)

  • You can specify options (arguments), each after a comma

  • Arguments are also called options

Pipe operator and function chaining

  • Pipe variant: >| (alternative notation shown in slides) where function(object) is equivalent to object |> function() (object is piped into the function)

  • In practice, the common pipe in R is %>% from the tidyverse

  • Pipe operators help link multiple functions on a single starting object

Where do functions come from?

  • Functions come from packages

  • Some are built-in (default)

  • Others you load (attach) or create yourself

  • Functions are kept in memory during an interactive session

Libraries and packages

  • Use library() to see installed packages in the class Cloud workspace or your PC, with brief descriptions

Working with packages

  • To install new packages: install.packages("package.name") (do this only once)

  • To load a package into the current session: library(package.name) (do this every time you start a new session and at the start of every .qmd)

Recap: Functions (key takeaways)

  • Functions are your actions in R

  • They operate on objects

  • They come from packages/libraries

  • They must be loaded for the function to be available

  • They have default settings but can be customized with options

  • You can pipe objects into functions

Preparing for next time: outline and materials

  • Next time covering Functions and data frame objects in R

  • Learning objectives:

    • Install and load packages to access functions

    • Implement functions to perform actions on data objects

    • Build a data frame from component vectors

Additional notes: lab materials and access

  • Weekly lab materials available on Posit Cloud at: https://posit.cloud/spaces/540014/

  • Initial join instructions (for first-time users) available at:

    • https://posit.cloud/spaces/540014/join?access_code=bhLxNeEgDTscoucYBR7QdXQrceqPnnJTNxNEOW9F

Next session reminder

  • Next time: Functions and data frame objects in R

  • Recap of learning objectives remains:

    • Install and load packages to access functions

    • Implement functions to perform actions on data objects

    • Build a data frame from component vectors