R02: R Basics

Meike Niederhausen and Nicky Wakim

2024-10-07

We will open RStudio on our computer (not R!)

Modern Dive

RStudio anatomy

Emma Rand

Let’s code! R Basics

Artwork by @allison_horst

Coding in the console

When you first open R, the console should be empty.

Typing and executing code in the console

  • Type code in the console (blue text)
  • Press return to execute the code
  • Output shown below in black

We have an option of where to write our code

  • We can use the console directly
    • BUT our work won’t be saved

 

  • We can also open up a file called a .R file
    • Hit the new document looking button, then click R script
    • Now we can type in the R script
    • In the R script, we need to press cmd + return or ctrl + return to execute the code
    • Output will show up in the Console!

 

  • Example by Nicky here!

Math calculations using R

  • We can use R as a calculator!!
  • Rules for order of operations are followed
  • Spaces between numbers and characters are ignored

 

10^2
[1] 100
3 ^ 7
[1] 2187
6/9
[1] 0.6666667
9-43
[1] -34
4^3-2* 7+9 /2
[1] 54.5

The equation above is computed as \[4^3 − (2 \cdot 7) + \frac{9}{2}\]