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().
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.
#
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.
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”.
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)
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")
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.
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.
myLogObject <- TRUE # Creating a logical object
myLogObject # Displaying the object
## [1] TRUE
class(myLogObject) # Displaying the class of the "myLogObject" object
## [1] "logical"
myNumObject <- 1 # Creating a numeric object
myNumObject # Displaying the object
## [1] 1
class(myNumObject) # Displaying the class of the "myNumObject" object
## [1] "numeric"
myCharObject <- "1" # Creating a character object
myCharObject # Displaying the object
## [1] "1"
class(myCharObject) # Displaying the class of the "myCharObject" object
## [1] "character"
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.
<- c("...","...")
First, we create a vector named fruits encompassing four character objects.
fruits <- c("banana", "apple", "watermelon", "kiwi") # Creating a character vector called fruits
fruits # Displaying the content of that vector
## [1] "banana" "apple" "watermelon" "kiwi"
We can also select a specific object within that vector using brackets. For instance, let’s select the object called “banana”.
fruits[1] # Displaying the first object within that vector
## [1] "banana"
data.frame()
We can create a data frame using the data.frame() function.
# First, let's create four vectors encompassing different types of food
fruits <- c("banana", "apple", "watermelon", "kiwi")
vegetables <- c("carrot", "cucumber", "potato", "tomato")
desserts <- c("ice cream", "cake", "donut", "cookie")
beverages <- c("coffee", "milk", "tea", "juice")
# Second, let's create a data frame using these vectors with the data.frame() function
MyData <- data.frame(fruits, vegetables, desserts, beverages, stringsAsFactors=FALSE)
# The following code would have created a similar data frame
MyData <- data.frame(
fruits=c("banana", "apple", "watermelon", "kiwi"),
vegetables=c("carrot", "cucumber", "potato", "tomato"),
desserts=c("ice cream", "cake", "donut", "cookie"),
beverages <- c("coffee", "milk", "tea", "juice"),
stringsAsFactors=FALSE)
You can select specific vectors or objects within this data frame using $ or brackets.
MyData[1,1] # Displaying the first object within the first vector
## [1] "banana"
MyData[2,1] # Displaying the second object within the first vector
## [1] "apple"
MyData$fruits # Displaying the column (or vector) called "fruits"
## [1] "banana" "apple" "watermelon" "kiwi"
MyData$fruits[1] <-"mango" # Changing the value of the first object within the "fruits"" vector
MyData$fruits[1] # Displaying the first object within the "fruits" vector
## [1] "mango"