BSTA 526: R Programming for Health Data Science
OHSU-PSU School of Public Health
2026-01-08
part01_b526_YOURNAMEorINITIALS.qmd and work from that.
By the end of this session, you should be able to:
R Intro from BSTA 511/611:
Read more about RStudio’s layout in Section 3.4 of “Getting Used to R, RStudio, and R Markdown” (Ismay and Kennedy 2016)
A good reference built into RStudio is Help -> Cheatsheets -> RStudio IDE cheat sheet
We will be using RStudio projects.
See Projects in RStudio webpage
Open RStudio by double clicking on the .Rproj file in the main OneDrive folder (BSTA_526_W26_class_materials_public.Rproj)
Ted Laderas’s short video on creating new projects: https://youtu.be/D22THnoPA6w
The grey box below is a code chunk:
# is called a comment and is not code that runs. It is useful for making notes for yourself.| action | mac | windows/linux |
|---|---|---|
| Run code in qmd or script | cmd + enter | ctrl + enter |
| Add code chunk | cmd + option + i | ctrl + alt + i |
<- |
option + - | alt + - |
| interrupt currently running code | esc | esc |
| in console, go to previously run code | up/down | up/down |
%>% |
cmd + shift + m | ctrl + shift + m |
| search files | cmd + shift + f | ctrl + shift + f |
| render qmd | cmd + shift + k | ctrl + shift + k |
| run entire code chunk | cmd + option + c | ctrl + alt + c |
| keyboard shortcut help | option + shift + k | alt + shift + k |
Below is an example of an R function:
R functions can have multiple arguments
Do we have to “name” the arguments?
Learn more about the round() function with ?round:
?round in the Console instead of including it in a code chunk.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.
There are many ways to get help. The more you learn how to get help, the easier your coding life will be. Here’s a list of options:
Post a question somewhere friendly:
#rstats on social media platformshist do?
+, which is actually a function?This happens when text is entered for a non-existent variable (object)
Can be due to missing quotes
or misspellings (R is case-sensitive)!
Change #| eval: false above to #| eval: true after you fix the code error.
day() being used below is from the lubridate package.
How do we fix this code?
[1] 9
# or load the package first (preferably at beginning of script)
library(lubridate)
day("2025-01-09")[1] 9
Or, maybe there was a misspelling…
<-<- is the primary assignment operator in R
weight_lb, but it doesn’t show us what the value is.() around the whole line of code to also see the value:weight_kg
weight_lb?remove() function: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:
c is for combine or concatenateIn the example above, each word within the vector is encased in quotation marks, indicating these are character data, rather than object names.
Please answer the following questions about organs:
organs?organs?Answers here:
TRUE and FALSE)NA indicates a missing value in R.NA is not a character!!!na.omit - be careful with this!![1] 2 4 4 6
attr(,"na.action")
[1] 4
attr(,"class")
[1] "omit"
Complete the following tasks after creating this vector (Note: there are multiple solutions):
more_heights (assign it to the object more_heights_complete)median() of more_heights_completeWe can see this when we try to add vectors together:
All mathematical and logical operators are vectorized functions:
[1] 49 81 121 169
[1] 8 10 12 14
[1] FALSE TRUE FALSE FALSE
[1] FALSE FALSE TRUE TRUE
[1] 0.1666667 0.2857143 0.3750000 0.4444444
But other common functions are as well:
From BSTA 511/611: R packages
library() or pacman::p_load()You can load packages with the
library() function orp_load() function in the pacman package.The following code loads two packages, though the tidyverse package is actually a suite of many packages.
I encourage you to start using pacman::p_load() in BSTA 526 instead of library().
Today we covered