In this recitation, we (1) introduce basic R functions, (2) create objects of different classes and (3) create vectors and data frames.

 

Relevant functions: install.packages(), library(), read.csv(), class(), data.frame().

 

 

1. Understanding the Basics

 

1.1 Running your code using command + enter

 

If you type code directly into the console, it will be executed automatically when you press enter. However, code is typically written in your script and sent into the console using command + enter at either extremity of the code line. You can also use the run icon in the RStudio environment, but this is less user-friendly.

 

answer <- 1+1 # Creating an object called answer 

answer # Displaying the value of our "answer" object
## [1] 2

 

Our initial code, displayed in pale grey, is a typical example of what can be written within a script. Similarly, ## [1] 2 is the output that would appear in your console by executing these code lines.

 

 

1.2 Writing a comment using #

 

Writing comments is an essential part of being a good data scientist. Not only does it make it easier for others to understand your code, but it also allows you to remember why you used specific functions. Characters following a # in your script are considered to be comments, and won’t be executed by R when you run your code.

 

# This is a comment

## This is also a comment

print("Hello World") # This is not a comment
## [1] "Hello World"

 

You can use as many # as you’d like to make your script more readable. Please note that if you use 5 # or more, you will create a chunk (or a code subsection). This technique can be useful when working with longer scripts.

 

 

1.3 Installing a package using install.packages()

 

R is an open-source programming language, which means that anyone with sufficient coding abilities can contribute to the R community by creating their own package. These packages work like “extension packs” for the basic functions which are already included when you first download R. install.packages() is one of these basic functions: it allows you to download any package listed in the R repository (https://cran.r-project.org/).

 

# install.packages("tidyverse") 

# Tidyverse is a collection of R packages designed for data science. 
# All packages share an underlying design philosophy, grammar, and data structure. 
# Please download this package, as it will be useful in this class. 
# For more information, see: https://www.tidyverse.org/packages/

 

Please note that R is sensitive to a variety of syntax elements. For instance, if you tried to run install.packages(tidyverse) (without “…”), an error message would appear. Likewise, R is case sensitive—so if you create an object called “myName”, the programming language won’t recognize “myname”.

 

 

1.4 Loading a package using library()

 

Once you install a package, it remains dormant within RStudio. In order to activate the functions encompassed within that package, you need to load it within your library using the library() function.

 

# library(tidyverse) 

 

 

1.5 Loading a dataset using read.csv()

 

Please note that you can load any .csv file within your working environment by using the read.csv() function. You need to indicate the correct path towards the file.

 

# turnout2018 <- read.csv("/Users/evelynebrie/Dropbox/Datasets/fileName.csv") 

 

 

2. Data Attributes and Structure

 

The following recitation material contains examples inspired from Yannick Dufresne’s “POL-7004 Analyse Quantitative” seminar.

 

Objects are entities with different attributes. A sequence of elements is called a vector, and a sequence of vectors is called a data frame.

 

(1) Element

 

(2) Vector

 

(3) Data frame

 

 

2.1 Creating an object and displaying its class using class()

 

Objects can have a variety of classes in R. In this recitation, we will focus on the three main classes: character, numeric and logical.

 

 

2.1.1 Logical object (= TRUE or FALSE)

 

myLogObject <- TRUE # Creating a logical object 
myLogObject # Displaying the object 
## [1] TRUE
class(myLogObject) # Displaying the class of the "myLogObject" object
## [1] "logical"

 

2.1.2 Numeric object (= numbers)

 

myNumObject <- 1 # Creating a numeric object 
myNumObject # Displaying the object 
## [1] 1
class(myNumObject) # Displaying the class of the "myNumObject" object
## [1] "numeric"

 

2.1.3 Character object (= anything within quotation marks)

 

myCharObject <- "1" # Creating a character object
myCharObject # Displaying the object 
## [1] "1"
class(myCharObject) # Displaying the class of the "myCharObject" object
## [1] "character"

 

2.2 Creating vectors and data frames

 

Let’s create a data frame of character objects. More precisely, we want to create this “grocery list” data frame with names of different things we wish to buy at the grocery store.