2  Basics

2.1 Objectives

  • Get comfortable with the RStudio IDE
  • Learn to use R as a calculator
  • Learn how to create variables
  • Get introduced to functions

2.2 Using RStudio

RStudio provides a user-friendly interface to the R programming language. There are 4 main panels that we will go over in the following sections: the file explorer, the console, the script window, and the environment window. Additionally, we will explore how to setup an R project to make using R Studio more convenient.

2.2.1 File Explorer

In the bottom right panel of RStudio, under the Files tab, you can navigate through the files on your computer. From this panel you can import datasets into R, access your R scripts, and set your working directory. Your working directory is the base folder you want to operate in within an R session. Any file paths or items saved in your environment will use your working directory as its base.

To set your working directory, navigate to the folder you want to be your working directory, click one More, and then Set As Working Directory. You can always change this, and we will discuss the best way to make use of a working directory in the Projects section below.

2.2.2 Console

In the bottom left panel of RStudio, under the Console tab, is where you will execute all of your R code. Each line of code is prompted by the > symbol. You do not need to type in the >, it is simply to indicate a new line of code. To execute your code you will type out your code and hit the Enter key on your keyboard. Below is an example of how to add 2 numbers on the console.

1 + 1
[1] 2

2.2.3 Scripts

In the top left panel of RStudio, you can open and edit files just like any other text editor. The primary file type you will use in R is a R script with the extension .r or .R. You can easily run lines from a script using Cmd + Enter on your keyboard or the Run button in the top right portion of this panel.

2.2.4 Environment

All variables you create will appear in the Environment panel in the top right corner of RStudio. This includes everything from a single number to a large data frame. This allows you to keep track of what you are creating. Most of the time, when you open RStudio, the environment from your previous session in that working directory is saved, but this is not guaranteed.

2.3 Using R as a Calculator

First, we will gain some experience using R for its most basic functionality, calculating!

2.3.1 Basic Calculations

You can add, subtract, divide, and multiply using the same symbols you would on a graphing calculator

(1 + 1)/4
[1] 0.5
(3*10)-5
[1] 25
2^3
[1] 8

2.3.2 Boolean Comparisons

You can evaluate boolean expressions, which return either TRUE or FALSE. The >, <, >=, <=, ==, &, and | operators are all valid.

1 == 1
[1] TRUE
2 < 1
[1] FALSE

The & and | operators are the AND and OR logical operators. The & operator evaluates to TRUE only if both sides of an expression are TRUE.

(1 == 1) & (2 < 1)
[1] FALSE
(1 == 1) & (2 > 1)
[1] TRUE

The | operator evaluates to true if either side of an expression are TRUE.

(1 == 1) | (2 < 1)
[1] TRUE
(1 == 1) | (2 > 1)
[1] TRUE

2.4 Assigning Variables

You can assign a variable in R using either the <- or = operator. A variable is a name that stores an object. The only limitation to variable names is that you cannot start with a number. For the most part, the code in this workshop uses <-, but you are welcome to use = if you prefer.

x <- 2
x
[1] 2
y = 2
y
[1] 2

2.5 Comments

As in most programming languages, you have the ability to add comments to your code. In R, the # symbol tells R to ignore everything after it. You can use comments to explain what is happening in your code so that you or someone else can read your really complex code a year later without too much effort. Comments are used throughout the code in this workshop to help you understand what each line does.

# This is a comment
z <- 3 # This is also a comment
z
[1] 3

As you can see, the comment did not interfere with setting the variable z.

2.6 Intro to Functions

A function is an object that takes arguments, processes them, and outputs whatever it calculated. R has a variety of built-in functions, but you can also build your own. We will dive deeper into how to build your own functions in Chapter 5. An example of a built-in function in R is the exp() function which takes 1 argument and places it as the exponent to \(e\). For example, we can calculate \(e^3 = 20.08554\) using:

exp(3)
[1] 20.08554

In this case, the argument is 3 and the return value is 20.08554.