Chapter 2 Part 1: Functions and Objects

2.1 Learning Objectives

By the end of this session, you should be able to:

  1. Work within the RStudio interface to run R code in an RMarkdown notebook
  2. Understand basic R syntax to use functions and assign values to objects
  3. Create and manipulate vectors and understand how R deals with missing data

2.2 An Intro to R/RStudio

[Tour of R and R Notebooks]

Help -> Cheetsheets -> RStudio IDE cheat sheet

2.2.1 Code Blocks

Now that we have our notebook open, we’re ready to start.

First thing of all, is the grey box below.

[Play button]

## [1] 9

Let’s learn how to read this code block. Everything that starts with a # is called a comment and is not code that runs. It is useful for making notes for youself

Below it is the actual code.

Try this one out. It’s the same code as above, but with no spaces. Does it still run?

## [1] 9

2.2.2 Using Functions

R includes functions for other types of math:

## [1] 3

[function names and arguments, syntax]

## [1] 3.1

[getting help]

[documentation and arguments]

## [1] 3.1

You may notice that boxes pop up as you type. These represent RStudio’s attempts to guess what you’re typing and share additional options.

2.2.3 Challenge 1:

What does the function hist do? What are its main arguments? How did you determine this?

2.2.4 Assigning objects

[objects and variables]

[assignment operator, object naming conventions]

Now that the object has been assigned, we can reference that object by executing its name:

## [1] 55

[using a variable]

## [1] 121

[assigning a new variable]

[environment panel]

[changing variable values]

[objects that use other objects don’t change]

You can think of the names of objects like sticky notes. You have the option to place the sticky note (name) on any value you choose. You can pick up the sticky note and place it on another value, but you need to explicitly tell R when you want values assigned to certain objects.

[removing objects]

You can clear the entire environment using the button at the top of the Environment panel with a picture of a broom. This may seem extreme, but don’t worry! We can re-create all the work we’ve already done by executing each line of code again.

2.2.5 Challenge 2:

What is the value of each item at each step? (Hint, you can see the value of an object by typing in the name of the object, such as with the mass line below.)

## [1] 47.5

Make your answers here:

2.3 Vectors

[creating vectors] [c is for combine]

## [1] 50 55 60 65

[learning things about vectors]

## [1] 4
## [1] "numeric"
## [1] 57.5
## [1] 50 65


In this case, each word is encased in quotation marks, indicating these are character data, rather than object names.

2.3.1 Challenge 3:

Please answer the following questions about organs:

  1. How many values are in organs?
  2. What type of data is organs?
  3. get overview of organs

Answers here:

2.3.2 Data types and Vectors

  • character: sometimes referred to as string data, tend to be surrounded by quotes
  • numeric: real or decimal numbers, sometimes referred to as “double”
  • integer: a subset of numeric in which numbers are stored as integers
  • logical: Boolean data (TRUE and FALSE)
  • complex: complex numbers with real and imaginary parts (e.g., 1 + 4i)
  • raw: bytes of data (machine readable, but not human readable)

2.3.3 Challenge 4:

R tends to handle interpreting data types in the background of most operations.

The following code is designed to cause some unexpected results in R.

What is unusual about each of the following objects?

2.3.5 Missing data

[NA is not a character]

## [1] NA
## [1] NA

[ugh. na strikes again!]

## [1] 4
## [1] 6
## [1] 2 4 4 6
## attr(,"na.action")
## [1] 4
## attr(,"class")
## [1] "omit"

2.3.6 Challenge:

Complete the following tasks after creating this vector (Note: there are multiple solutions): (solutions here)

  1. Remove NAs on more_heights (assign it to the object more_heights_complete)
  2. Calculate the median() of more_heights_complete

2.4 Wrapping up

[R/Rstudio] [objects] [data types]

2.5 Assignment

  1. Object manipulation

Create an object called agge that contains your age in years. Then reassign the object to a new object called age (e.g., correct the typo). Then remove the previous object from your environment and then calculate your age in days

  1. Vector manipulation (character data):

Create a object called buildings representing a vector that contains four names of buildings on OHSU’s campus, including the building where you work (here’s a reference:

Add Portland, Oregon to the beginning of the vector, and Phys Plant to the end of the vector

subset the vector to show only the building in which you work.

  1. Vector manipulation (numerical data):

The following vector represents the number of vacation days possessed by various employees.

How many employees are represented in the vector?

How many vacation days total?

2.6 Submitting your homework

Submit your part1-FIRSTNAME-LASTNAME.nb.html file in Sakai. To download it, click on the More gear icon, and click Export to download it to your computer.