Chapter 2 Part 1: Functions and Objects
2.1 Learning Objectives
By the end of this session, you should be able to:
- Work within the RStudio interface to run R code in an RMarkdown notebook
- Understand basic R syntax to use functions and assign values to objects
- 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.
##  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?
##  9
2.2.2 Using Functions
R includes functions for other types of math:
##  3
[function names and arguments, syntax]
##  3.1
[documentation and arguments]
##  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:
##  55
[using a variable]
##  121
[assigning a new variable]
[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.
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.)
##  47.5
width <- 122 # 2. width? mass <- mass * 2.0 # 3. mass? width <- width - 20 #4. width? mass_index <- mass/width # 5. mass_index?
Make your answers here:
[creating vectors] [c is for combine]
##  50 55 60 65
[learning things about vectors]
##  4
##  "numeric"
##  57.5
##  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
- How many values are in
- What type of data is
- get overview of
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.4 Manipulating vectors
##  "prostate"
##  "lung" "breast"
##  "lung" "breast"
2.3.5 Missing data
[NA is not a character]
##  NA
##  NA
[ugh. na strikes again!]
##  4
##  6
##  2 4 4 6 ## attr(,"na.action") ##  4 ## attr(,"class") ##  "omit"
Complete the following tasks after creating this vector (Note: there are multiple solutions): (solutions here)
- Remove NAs on
more_heights(assign it to the object
- Calculate the
2.4 Wrapping up
[R/Rstudio] [objects] [data types]
- 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
#create agge object #reassign agge value to age #remove agge object #Use math to calculate your age in days
- 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: https://www.ohsu.edu/visit/maps).
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.
#create buildings object buildings <- c() #Add "Portland, Oregon" to the beginning of buildings #Add "Phys Plant" to the end of buildings #subset buildings to show only your building buildings
- 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
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.