+ - 0:00:00
Notes for current slide
Notes for next slide

Module 01: Getting Started with R, RStudio, R Objects

CDC R Workshop April 21-22, 2022

Melinda Higgins

Director Biostatistics & Data Core
School of Nursing - Emory University
04/21/2022

1 / 33
2 / 33

Overview

This workshop will cover:

  • An introduction to R and RStudio
  • Learning about R Objects and data structures
  • Getting data into and out of R
  • Data Wrangling
  • Data Visualization (e.g. ggplot2)
  • Basic statistical analyses and models
  • Brief intro to simple choropleth maps
  • Brief Intro to RMarkdown
  • [time permitting]
    • data joins and restructuring
    • introduction to linear and logistic regression
    • other topics as requested by attendees
3 / 33

DAY 1 SCHEDULE

: April 21, 2022
🕘: 09:00 - 17:30

Schedule

Time Activity
09:00 - 10:15 Module 1
10:15 - 10:30 Break
10:30 - 12:00 Module 2
12:00 - 13:00 LUNCH
13:00 - 14:15 Module 3
14:15 - 14:25 Break
14:25 - 15:30 Module 4
15:30 - 15:40 Break
15:40 - 17:00 Module 5
17:00 - 17:30 Post workshop Q&A; one-on-one help
4 / 33

A few quick poll questions:

[1] Do you have any experience with R? [pick 1 response]

5 / 33

A few quick poll questions:

[2] What do you want to learn during this R Workshop? [free text, can enter multiple responses]

6 / 33

A few quick poll questions:

[3] What intimidates you about learning R? [free text, can enter multiple responses]

7 / 33

R + RStudio

Plain R Console

Old Fashioned "command line"

RStudio IDE

Integrated Development Environment

NOTE: This is NOT a GUI

8 / 33
9 / 33
10 / 33
11 / 33
12 / 33
13 / 33
14 / 33

Let's get started!!

1. Open RStudio

15 / 33

Create a new project

Go to File/New Project

16 / 33

Create a new project

Choose New Directory

17 / 33

Create a new project

Click New Project

18 / 33

Create a new project

Type in folder name and choose parent directory

19 / 33

What is a RStudio "Project"?

  • A "container" or aka a directory with all files for this RStudio Project
20 / 33

What is a RStudio "Project"?

  • A "container" or aka a directory with all files for this RStudio Project
  • Good Coding Practice!!
20 / 33

What is a RStudio "Project"?

  • A "container" or aka a directory with all files for this RStudio Project
  • Good Coding Practice!!
  • The DEFAULT place RStudio looks for your files
20 / 33

Your new Rworkshop project

21 / 33

Get files for the Workshop

  1. Go to https://melindahiggins2000.github.io/CDC_Rworkshop_April2022/

  2. Download module01_Rscript.R and put it into the new folder on your computer, e.g. C:\Rworkshop.

22 / 33

Files in your Rworkshop folder on your computer

23 / 33

Files in your Rworkshop folder/RStudio project

Click on module01_Rscript.R to open it.

24 / 33

Type code at command line or run one line at a time from script window.

25 / 33

Basic commands done in Console

26 / 33

Code lines run in the console or from script window

4 + 4
[1] 8
26 / 33

Code lines run in the console or from script window

4 + 4
sqrt(25)
[1] 8
[1] 5
26 / 33

Code lines run in the console or from script window

4 + 4
sqrt(25)
pi
[1] 8
[1] 5
[1] 3.141593
26 / 33

Code lines run in the console or from script window

4 + 4
sqrt(25)
pi
seq(from=1, to=10, by=0.5)
[1] 8
[1] 5
[1] 3.141593
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
[16] 8.5 9.0 9.5 10.0
26 / 33

ASSIGN your results to an "object", use <-

We really don't want to keep typing one line at a time in the Console, so we need to save/store value(s) in an object. An object is a text string we use to retrieve the value (or elements) we saved. Let's save the results of 2+2 in an object result.

For this we will use the ASSIGN operator <-.

result <- 2+2
27 / 33

ASSIGN your results to an "object", use <-

We really don't want to keep typing one line at a time in the Console, so we need to save/store value(s) in an object. An object is a text string we use to retrieve the value (or elements) we saved. Let's save the results of 2+2 in an object result.

For this we will use the ASSIGN operator <-.

result <- 2+2

This is saved now in your "Global Environment". To retrieve this value, simply type the text string result to see the value 4.

result
[1] 4
27 / 33

ASSIGN your results to an "object", use <-

We really don't want to keep typing one line at a time in the Console, so we need to save/store value(s) in an object. An object is a text string we use to retrieve the value (or elements) we saved. Let's save the results of 2+2 in an object result.

For this we will use the ASSIGN operator <-.

result <- 2+2

This is saved now in your "Global Environment". To retrieve this value, simply type the text string result to see the value 4.

result
[1] 4

globalenv

27 / 33

Your Turn

1. Start RStudio - Open "module01_Rscript.R"

2. Highlight Run Code for EXERCISE 01

28 / 33

How to "run" code in an R script

29 / 33

Create Objects and Use Them

# save sequence of numbers in object x
x <- seq(from=1, to=10, by=0.5)
29 / 33

Create Objects and Use Them

# save sequence of numbers in object x
x <- seq(from=1, to=10, by=0.5)
# view the contents of x
x
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
[16] 8.5 9.0 9.5 10.0
29 / 33

Create Objects and Use Them

# save sequence of numbers in object x
x <- seq(from=1, to=10, by=0.5)
# view the contents of x
x
# use x to create new object y
y <- x*x
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
[16] 8.5 9.0 9.5 10.0
29 / 33

Create Objects and Use Them

# save sequence of numbers in object x
x <- seq(from=1, to=10, by=0.5)
# view the contents of x
x
# use x to create new object y
y <- x*x
# plot x and y
plot(x,y)
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
[16] 8.5 9.0 9.5 10.0

29 / 33

Questions

  1. How many objects are in your "Environment" [top right window]

  2. What kind of object is x?

  3. How many elements does y have in it?

  4. Where do you see the plot?

30 / 33

Questions

Getting Help

To find out what the seq() function does, in the Console window [bottom left] at the prompt > type:

help(seq)

seq(from=1, to=10, by=0.5)
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
[16] 8.5 9.0 9.5 10.0

Notice that we used equals = inside the () to assign values to the arguments for the seq() function.

31 / 33

Your Turn

1. Open "module01_Rscript.R"

2. Highlight Run Code for EXERCISE 02

3. Write Code to make plot for tangent of x

4. Write Code to make plot of log of x with log y

ZOOM BREAKOUT, 5 MIN

32 / 33

BREAK TILL 10:30 AM

33 / 33
2 / 33
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
oTile View: Overview of Slides
Esc Back to slideshow