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

Basics of R & RStudio

1 / 42

RStudio IDE

2 / 42

RStudio IDE

3 / 42

RStudio IDE

4 / 42

RStudio IDE

5 / 42

RStudio IDE

6 / 42

RStudio IDE

7 / 42

RStudio IDE


















Let's take a look at the IDE in RStudio Cloud...

8 / 42

Basic computing

# Addition
2 + 3
## [1] 5
# Subtraction
2 - 3
## [1] -1
# Multiplication
2 * 3
## [1] 6
# Division
10/2
## [1] 5
9 / 42

Basic computing

# Exponents
4^2
## [1] 16
# Roots
4^(1/2)
## [1] 2
10 / 42

Basic computing

# Exponents
4^2
## [1] 16
# Roots
4^(1/2)
## [1] 2

Thankfully, R follows the order of operations (PEMDAS).

2^3 + 4 * 1/2
## [1] 10
11 / 42

Basic computing

# Exponents
4^2
## [1] 16
# Roots
4^(1/2)
## [1] 2

Thankfully, R follows the order of operations (PEMDAS).

2^3 + 4 * 1/2
## [1] 10

These values haven't been stored anywhere though...

12 / 42

Storing values in objects

To keep a value in memory, we need to assign it to an object.

13 / 42

Storing values in objects

To keep a value in memory, we need to assign it to an object.

To assign a value to an object, use the assignment operator. It looks like a left-pointing arrow:

<-

14 / 42

Storing values in objects

To keep a value in memory, we need to assign it to an object.

To assign a value to an object, use the assignment operator. It looks like a left-pointing arrow:

<-


Let's use <- to create an object called x, that is assigned the number 8.

x <- 8
15 / 42

Storing values in objects

To keep a value in memory, we need to assign it to an object.

To assign a value to an object, use the assignment operator. It looks like a left-pointing arrow:

<-


Let's use <- to create an object called x, that is assigned the number 8.

x <- 8

Now we can call x by name, and it will print the value.

x
## [1] 8
16 / 42

Style

Technically, both <- and = work as assignment operators. But we'll strictly use <- from here on out.

17 / 42

Style

Technically, both <- and = work as assignment operators. But we'll strictly use <- from here on out.


This brings up a general coding principle of the day...

18 / 42

Style

Technically, both <- and = work as assignment operators. But we'll strictly use <- from here on out.


This brings up a general coding principle of the day...

Style is important!

You want to use a consistent style so that others (including your future self) can easily and quickly read your code.

19 / 42

Style

Technically, both <- and = work as assignment operators. But we'll strictly use <- from here on out.


This brings up a general coding principle of the day...

Style is important!

You want to use a consistent style so that others (including your future self) can easily and quickly read your code.

I highly recommend following the tidyverse style guide. We'll talk more about the tidyverse tomorrow

20 / 42

Style

Technically, both <- and = work as assignment operators. But we'll strictly use <- from here on out.


This brings up a general coding principle of the day...

Style is important!

You want to use a consistent style so that others (including your future self) can easily and quickly read your code.

I highly recommend following the tidyverse style guide. We'll talk more about the tidyverse tomorrow


Hint: You will type <- a LOT. The keyboard shortcut Alt+- or Option+- can be used to insert a <-.

21 / 42

Storing values in objects

Now, let's take a look at the variable y...

22 / 42

Storing values in objects

Now, let's take a look at the variable y...

# call y
y
## Error in eval(expr, envir, enclos): object 'y' not found
23 / 42

Storing values in objects

Now, let's take a look at the variable y...

# call y
y
## Error in eval(expr, envir, enclos): object 'y' not found

Whoops! We didn't assign anything to y. Calling a variable that doesn't exist leads to an error message.

24 / 42

Storing values in objects

Now, let's take a look at the variable y...

# call y
y
## Error in eval(expr, envir, enclos): object 'y' not found

Whoops! We didn't assign anything to y. Calling a variable that doesn't exist leads to an error message.

You'll see a LOT of error messages when using R. Don't worry when this happens. This is 100% normal, and it happens to everyone all the time.

25 / 42

Storing values in objects

Now, let's take a look at the variable y...

# call y
y
## Error in eval(expr, envir, enclos): object 'y' not found

Whoops! We didn't assign anything to y. Calling a variable that doesn't exist leads to an error message.

You'll see a LOT of error messages when using R. Don't worry when this happens. This is 100% normal, and it happens to everyone all the time.


# Need to assign something to y!
y <- 2
y
## [1] 2
26 / 42

Storing values in objects

Now, we can use these variables in calculations

27 / 42

Storing values in objects

Now, we can use these variables in calculations

Remember, x <- 8 and y <- 2

# multiply them
x * y
## [1] 16
28 / 42

Storing values in objects

Now, we can use these variables in calculations

Remember, x <- 8 and y <- 2

# multiply them
x * y
## [1] 16

But remember, if we want to save the output of that calculation, we need to assign it to an object!

# define the product as z
z <- x * y
# print z
z
## [1] 16
29 / 42

Naming variables

You may be wondering how to name variables in R. There are just a few rules...

30 / 42

Naming variables

You may be wondering how to name variables in R. There are just a few rules...

  1. object names must start with a letter

  2. can contain alphanumeric characters, "_", and "."

  3. R is case sensitive, so A and a would be different variables

31 / 42

Naming variables

You may be wondering how to name variables in R. There are just a few rules...

  1. object names must start with a letter

  2. can contain alphanumeric characters, "_", and "."

  3. R is case sensitive, so A and a would be different variables


This is another element of style. Using a consistent style when naming variables makes your life easier. There are several to choose from.

32 / 42

Naming variables

Artwork by @allison_horst

Some examples

  • this_is_snake_case
  • thisIsCamelCase
  • avoid.using.periods

I'll be using snake_case from here on, but you can change to another option if you'd like.

33 / 42

Your turn 1

01:30

Open the file your_turn.Rmd inside the project 01_rstudio_basics on RStudio Cloud.

  1. Create a variable called var_1 that is equal to 4.

  2. Then create a variable called var_2 that is var_1 raised to the power of 6. Print the results (i.e. check what value is assigned to var_2).

34 / 42

Solution

# Q1.
var_1 <- 4
# Q2.
var_2 <- var_1^6
var_2
## [1] 4096
35 / 42

R Markdown

Artwork by @allison_horst

R Markdown is a magical thing. It allows you to combine R code and plain text together in the same document.

R Markdown documents end with the extension .Rmd. Let's take a look at how they work...

36 / 42

R Markdown

Image from Kieran Healy

37 / 42

Your turn 2

02:00
  1. Open the file rmarkdown_example.Rmd and click knit. Take a look at the resulting file called rmarkdown_example.html.

  2. Go back to rmarkdown_example.Rmd. Change the title to "[Your name]'s example R Markdown Document" and re-knit.

38 / 42

Solution

39 / 42

Q & A

05:00
40 / 42

Next up...

Data Types & Structures

41 / 42

Break!

10:00
42 / 42

RStudio IDE

2 / 42
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