L2: Introduction to R, Quarto, and R evironments

Bogdan G. Popescu

John Cabot University

Installing R and R Studio

We will install two programs

  • R
  • R Studio

Installing R and R Studio

We can do that by going to: https://posit.co/download/rstudio-desktop/

Installing R Studio

We can do that by going to: https://posit.co/download/rstudio-desktop/

Install the version of RStudio relevant for your OS.

Note that there are different files for Apple silicon (M1/M2) Macs and for Intel Macs

Installing R Studio

We can do that by going to: https://posit.co/download/rstudio-desktop/

Install the version of RStudio relevant for your OS.

Note that there are different files for Apple silicon (M1/M2) Macs and for Intel Macs.

R panels

The platform interface for R studio looks like below:

R panels

The platform interface for R studio looks like below:

R panels

The platform interface for R studio looks like below:

R panels

The platform interface for R studio looks like below:

Quarto Files

Quarto is a version of R Markdown from RStudio that allows us to run code and write text.

Quarto files have the *.qmd extension

Quarto Files

You can produce a wide variety of output types:

  • executable code blocks
  • plots
  • tabular output from data frames
  • plain text output

Installation

You can now start typing.

To use Quarto with R, you should install the rmarkdown R package:

install.packages("rmarkdown")

Installation

You can now start typing.

To use Quarto with R, you should install the rmarkdown R package:

Installation

You can now start typing.

To use Quarto with R, you should install the rmarkdown R package:

Installation

You can now start typing.

To use Quarto with R, you should install the rmarkdown R package:

Using R

Let us now use R to understand how it works.

Let’s create a new quarto document and save it in your “week2” folder

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

  • R can be used as a simple calculator and we can perform any simple computation.

Using R

  • R can be used as a simple calculator and we can perform any simple computation.
# Sample Session

Using R

  • R can be used as a simple calculator and we can perform any simple computation.
# Sample Session
# This is a comment

Using R

  • R can be used as a simple calculator and we can perform any simple computation.
# Sample Session
# This is a comment
# print a number
2 
[1] 2

Using R

Using R

Using R

Using R

  • R can be used as a simple calculator and we can perform any simple computation.
# Sample Session
# This is a comment
# print a number
2
[1] 2
# perform a simple calculation
2+3
[1] 5

After we type 2+3 and press Enter, 2+3 is sent to your computer’s processor.

Using R

  • R can be used as a simple calculator and we can perform any simple computation.
# Sample Session
# This is a comment
# print a number
2
[1] 2
# perform a simple calculation
2+3
[1] 5

After we type 2+3 and press Enter, 2+3 is sent to your computer’s processor.

The returned value 5 is then printed in the console

Using R

  • R can be used as a simple calculator and we can perform any simple computation.
# Sample Session
# This is a comment
# print a number
2
[1] 2
# perform a simple calculation
2+3
[1] 5

After we type 2+3 and press Enter, 2+3 is sent to your computer’s processor.

The returned value 5 is then printed in the console

Note that this is not kept in the RAM memory or stored.

Using R

  • R can be used as a simple calculator and we can perform any simple computation.
# Sample Session
# This is a comment
# print a number
2
[1] 2
# perform a simple calculation
2+3
[1] 5

After we type 2+3 and press Enter, 2+3 is sent to your computer’s processor.

The returned value 5 is then printed in the console

Note that this is not kept in the RAM memory or stored.

5 is simply printed in the console.

Using R

Using R

Using R

Using R

Using R

Using R

Quarto Notebooks and Presentations

You can create Quarto Notebooks and presentations easily.

You simply need to change the preamble.

Quarto Notebooks

This is for example the preamble for course syllabus

---
title: "Applied Computational Methods for Social Sciences"
author: "Bogdan G. Popescu"
layout: archive
format:
  html:
    toc: true
    number-sections: true
    colorlinks: true
    smooth-scroll: true
    embed-resources: true
author_profile: yes
permalink: /teaching/
---

This is an example.

Quarto Notebooks

This is for example the preamble for course syllabus

Quarto Notebooks

This is for example the preamble for course syllabus

Quarto Notebooks

This is the output:

Quarto Presentations

This is for example the preamble for the the presentation for today.

---
title: "L2: Introduction to R, Quarto, and R evironments"
author:
  name: Bogdan G. Popescu
  email: bogdan.popescu@johncabot.edu
  affiliations: John Cabot University
format:
  revealjs:
    slide-number: c/t
    show-slide-number: all
    preview-links: auto
    width: 1050
    height: 700
    fontsize: 24pt
    footer: "Popescu (JCU): Lecture 2"
    sansfont: Latin Modern Roman
    embed-resources: true
---
      
# Slide 1

This is an example

# Slide 2

This is another example

Quarto Presentations

This is for example the preamble for presentation today

Quarto Presentations

This is for example the preamble for presentation today

Quarto Presentations

This is the output for slide 1

Quarto Presentations

This is the output for slide 2

Quarto Presentations

This is the output for slide 3

Numeric and string objects

# store a numeric object
x <- 2 

Numeric and string objects

# store a numeric object
x <- 2 
# print this numeric object
x
[1] 2

Numeric and string objects

# store a numeric object
x < 2 
[1] FALSE
# print this numeric object
x
[1] 2
# store a string object
x <- "Hello"

Numeric and string objects

# store a numeric object
x <- 2 
# print this numeric object
x
[1] 2
# store a string object
x <- "Hello"
x
[1] "Hello"

Numeric and string objects

# store a numeric object
x <- 2 
# print this numeric object
x
[1] 2
# store a string object
x <- "Hello"
x
[1] "Hello"
# we can also write
x <- 'Hello'
x
[1] "Hello"

Numeric and string objects

# store a numeric object
x <- 2 
# print this numeric object
x
[1] 2
# store a string object
x <- "Hello"
x
[1] "Hello"
# we can also write
x <- 'Hello'
x
[1] "Hello"

Note that when x is 1 or 3, we say that x is a numeric object

Numeric and string objects

# store a numeric object
x <- 2 
# print this numeric object
x
[1] 2
# store a string object
x <- "Hello"
x
[1] "Hello"
# we can also write
x <- 'Hello'
x
[1] "Hello"

Note that when x is 1 or 3, we say that x is a numeric object

When x is "Hello" or 'Hello', we say that x is a string object.

Arithmetic operations

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponent

Arithmetic operations

Here are examples of this:

# Addition
5+3
[1] 8

Arithmetic operations

Here are examples of this:

# Addition
5+3
[1] 8
#Subtraction
4-5
[1] -1

Arithmetic operations

Here are examples of this:

# Addition
5+3
[1] 8
#Subtraction
4-5
[1] -1
#Multiplication
2*3
[1] 6

Arithmetic operations

Here are examples of this:

# Addition
5+3
[1] 8
#Subtraction
4-5
[1] -1
#Multiplication
2*3
[1] 6
#Division
3/3
[1] 1

Arithmetic operations

Here are examples of this:

# Addition
5+3
[1] 8
#Subtraction
4-5
[1] -1
#Multiplication
2*3
[1] 6
#Division
3/3
[1] 1
#Exponent
5^2
[1] 25

Arithmetic operations

Note that numbers that are either very large or very small, R uses scientific notation

Example:

1 / 1000000
[1] 1e-06

Note that 1000000 has 6 zeros.

This can also be written as \(1*10^{-6}\) or \((\frac{1}{10})^6\)

Thus, the output produced by R - 1e-06 makes sense.

Arithmetic operations

Infinity is treated as a special type of number: Inf or -Inf.

Here are examples:

1/0
[1] Inf

This results in Inf: division by zero is undefined

In the limit, as a number approaches zero, its reciprocal becomes larger and larger, eventually reaching infinity.

Another example:

-1 / 0
[1] -Inf

Conditional Operators

Conditions are expressions that use conditional operators and that have TRUE or FALSE as a result

The conditional operators available in R are listed below:

Operator Meaning
== Equal
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
!= Not equal
&, | And, Or

Conditional Operators Examples

We can use conditional operators in the following examples:

1 < 2
[1] TRUE

Conditional Operators Examples

We can use conditional operators in the following examples:

1 < 2
[1] TRUE
2 > 2
[1] FALSE

Conditional Operators Examples

We can use conditional operators in the following examples:

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

Conditional Operators Examples

We can use conditional operators in the following examples:

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

or

(1 < 10) & (10 < 100)
[1] TRUE

Conditional Operators Examples

We can use conditional operators in the following examples:

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

or

(1 < 10) & (10 < 100)
[1] TRUE
(1 < 10) | (10 < 100)
[1] TRUE

Conditional Operators Examples

We can use conditional operators in the following examples:

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

or

(1 < 10) & (10 < 100)
[1] TRUE
(1 < 10) | (10 < 100)
[1] TRUE

or

(1 == 1) & !(2 == 2)
[1] FALSE

Special Values

Operator Meaning
Inf Infinity
NA Not Available
NaN Not a Number
NULL Empty object

Classes

R is an object-oriented language where each object belongs to a class

The class function accepts an object and returns a class name

class(TRUE)
[1] "logical"
class(1)
[1] "numeric"
class("a")
[1] "character"
class("sqrt")
[1] "character"
class("1 < 2")
[1] "character"

Vectors

The vector is the simplest data structure in R.

Vectors

The vector is the simplest data structure in R.

It is an ordered collection of values of the same type:

Vectors

The vector is the simplest data structure in R.

It is an ordered collection of values of the same type:

  • Numbers - numeric (numbers with a decimal point) or integer (whole numbers)
  • Text - character
  • Logical - logical

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

# Storing a numeric vector
vector1<-600

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

# Storing a numeric vector
vector1<-600
vector1
[1] 600

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

# Storing a numeric vector
vector1<-600
length(vector1)
[1] 1

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

# Storing another character vector

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

# Storing another character vector
vector2<-"Hello"

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

# Storing another character vector
vector2<-"Hello"
vector2
[1] "Hello"

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

# Storing another character vector
vector2<-"Hello"
length(vector2)
[1] 1

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

# Storing another logical vector
vector3<-TRUE

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

# Storing another logical vector
vector3<-TRUE
vector3
[1] TRUE

The c function

A vector of length 1 can be created by typing 600, "Hello" or TRUE.

# Storing another logical vector
vector3<-TRUE
length(vector3)
[1] 1

Vectors

A vector of length > 1 can be achieved by using the c function which various inputs

Vectors

A vector of length > 1 can be achieved by using the c function which various inputs

For example we can create a vector of length 2

Vectors

A vector of length > 1 can be achieved by using the c function which various inputs

For example we can create a vector of length 2

# Storing a vector of length 2

Vectors

A vector of length > 1 can be achieved by using the c function which various inputs

For example we can create a vector of length 2

# Storing a vector of length 2
vector4<-c(1, 2)

Vectors

A vector of length > 1 can be achieved by using the c function which various inputs

For example we can create a vector of length 2

# Storing a vector of length 2
vector4<-c(1, 2)
vector4
[1] 1 2

Vectors

A vector of length > 1 can be achieved by using the c function which various inputs

For example we can create a vector of length 2

# Storing a vector of length 2
vector4<-c(1, 2)
length(vector4)
[1] 2

Vectors of different types

We can also create create a vector made out of different elements

Vectors of different types

We can also create create a vector made out of different elements

# Storing a vector of with elements of different types

Vectors of different types

We can also create create a vector made out of different elements

# Storing a vector of with elements of different types
x<-c(1, "Hello")

Vectors of different types

We can also create create a vector made out of different elements

# Storing a vector of with elements of different types
x<-c(1, "Hello")
x
[1] "1"     "Hello"

Vectors of character elements

We can also create create a vector made out of character elements

Vectors of character elements

We can also create create a vector made out of character elements

# Storing a vector of with character elements
x2<-c("cat", "dog", "mouse", "apple")

Vectors of character elements

We can also create create a vector made out of character elements

# Storing a vector of with character elements
x2<-c("cat", "dog", "mouse", "apple")
x2
[1] "cat"   "dog"   "mouse" "apple"

Vectors subsetting

# Storing a vector
height <- c(168, 177, 177, 177, 178, 172, 165, 171, 178, 170)

Vectors subsetting

# Storing a vector
height <- c(168, 177, 177, 177, 178, 172, 165, 171, 178, 170)

# Printing the second component
height[2]
[1] 177

Vectors subsetting

# Storing a vector
height <- c(168, 177, 177, 177, 178, 172, 165, 171, 178, 170)

# Printing the second component
height[2]
[1] 177
# Printing the second, the 3rd, the 4th and 5th component
height[2:5]
[1] 177 177 177 178

Operations with Vectors

# This is another vector 
weight <- c(88, 72, 85, 52, 71, 69, 61, 61, 51, 75)

Operations with Vectors

# This is another vector 
weight <- c(88, 72, 85, 52, 71, 69, 61, 61, 51, 75)
# Performing a simple calculation using vectors
bmi = weight/((height/100)^2)

Operations with Vectors

# This is another vector 
weight <- c(88, 72, 85, 52, 71, 69, 61, 61, 51, 75)
# Performing a simple calculation using vectors
bmi = weight/((height/100)^2)
bmi
 [1] 31.17914 22.98190 27.13141 16.59804 22.40879 23.32342 22.40588 20.86112
 [9] 16.09645 25.95156

Describing Vectors

# Measuring the length of a vector
length(weight)
[1] 10

Describing Vectors

# Measuring the length of a vector
length(weight)
[1] 10
# Calculating the mean of a vector
mean(weight)
[1] 68.5

Describing Vectors

# Measuring the length of a vector
length(weight)
[1] 10
# Calculating the mean of a vector
mean(weight)
[1] 68.5
# Calculating the variance of a vector
var(weight)
[1] 156.0556

Consecutive vectors

Other than the c function there are three additional operations for creating vectors:

  • The : operator
  • The seq function
  • The rep function

The : operator

We can create a vector with consecutive numbers:

x<-c(1:5)

The : operator

We can create a vector with consecutive numbers:

x<-c(1:5)
x
[1] 1 2 3 4 5

The seq function

The seq function gives more flexibility by introducing three additional paramters.

The seq function

The seq function gives more flexibility by introducing three additional paramters.

  • from - where to start

The seq function

The seq function gives more flexibility by introducing three additional paramters.

  • from - where to start
  • to - where to end

The seq function

The seq function gives more flexibility by introducing three additional paramters.

  • from - where to start
  • to - where to end
  • by - step size

The seq function

The seq function gives more flexibility by introducing three additional paramters.

  • from - where to start
  • to - where to end
  • by - step size
x2<-seq(from = 1, to = 5, by = 1)

The seq function

The seq function gives more flexibility by introducing three additional paramters.

  • from - where to start
  • to - where to end
  • by - step size
x2<-seq(from = 1, to = 5, by = 1)
x2
[1] 1 2 3 4 5

The seq function

The seq function gives more flexibility by introducing three additional paramters.

  • from - where to start
  • to - where to end
  • by - step size
x3<-seq(from = 1, to = 5, by = 2)

The seq function

The seq function gives more flexibility by introducing three additional paramters.

  • from - where to start
  • to - where to end
  • by - step size
x3<-seq(from = 1, to = 5, by = 2)
x3
[1] 1 3 5

Repetitive Vectors

The rep function replicates its argument to create a repetitive vector:

Repetitive Vectors

The rep function replicates its argument to create a repetitive vector:

  • x - the object to replicate

Repetitive Vectors

The rep function replicates its argument to create a repetitive vector:

  • x - the object to replicate
  • times - how many times to replicate the vector

Repetitive Vectors

The rep function replicates its argument to create a repetitive vector:

  • x - the object to replicate
  • times - how many times to replicate the vector
  • each - how many times to replicate each element

Repetitive Vectors

The rep function replicates its argument to create a repetitive vector:

  • x - the object to replicate
  • times - how many times to replicate the vector
  • each - how many times to replicate each element
x4<-rep(x = 2, times = 5)

Repetitive Vectors

The rep function replicates its argument to create a repetitive vector:

  • x - the object to replicate
  • times - how many times to replicate the vector
  • each - how many times to replicate each element
x4<-rep(x = 2, times = 5)
x4
[1] 2 2 2 2 2

Missing Values

The is.na is the function to detect missing NA values

The is.na accepts any vector

It returns a logical vector, with TRUE in place of NA values and FALSE in place of non-NA values.

Example:

x = c(28, 58, NA, 31, 39, NA, 9)
x
[1] 28 58 NA 31 39 NA  9

Missing Values

This is how we detect NAs

x = c(28, 58, NA, 31, 39, NA, 9)
is.na(x)
[1] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE

Many functions such as sum and mean have an na.rm parameters to exclude NA values from the calculation

Example:

mean(x)
[1] NA
mean(x, na.rm=T)
[1] 33

The sort function

The sort function returns ordered vector indices

For example:

x<-c(1, 2, 5, 7, 4, 6)
sort(x, decreasing = FALSE)
[1] 1 2 4 5 6 7

The paste and paste0 function

  • The paste function is used to “paste” text values.

  • The sep determines the separating character with the default being sep="".

Example:

paste("There are", "5", "books.")
[1] "There are 5 books."

Alternatively:

paste("There are", "5", "books.", sep = "_")
[1] "There are_5_books."

The paste and paste0 function

We can use paste to obtain names of files

paste("image", 1:5, ".tif", sep = "")
[1] "image1.tif" "image2.tif" "image3.tif" "image4.tif" "image5.tif"

The paste and paste0 function

paste() is like concatenation using separation factor

paste0() is like append function using separation factor

Examples:

paste("a","b")
[1] "a b"
paste0("a","b")
[1] "ab"
paste("a","b",sep="-")
[1] "a-b"
paste0("a","b",sep="-")
[1] "ab-"

Matrices

# This is another vector 
weight <- c(88, 72, 85, 52, 71, 69, 61, 61, 51, 75)
height <- c(168, 177, 177, 177, 178, 172, 165, 171, 178, 170)
# Performing a simple calculation using vectors
bmi = weight/((height/100)^2)

Matrices

matrix <- cbind(height, weight, bmi)

Matrices

matrix <- cbind(height, weight, bmi)
matrix
      height weight      bmi
 [1,]    168     88 31.17914
 [2,]    177     72 22.98190
 [3,]    177     85 27.13141
 [4,]    177     52 16.59804
 [5,]    178     71 22.40879
 [6,]    172     69 23.32342
 [7,]    165     61 22.40588
 [8,]    171     61 20.86112
 [9,]    178     51 16.09645
[10,]    170     75 25.95156

Matrices

matrix <- cbind(height, weight, bmi)
matrix
      height weight      bmi
 [1,]    168     88 31.17914
 [2,]    177     72 22.98190
 [3,]    177     85 27.13141
 [4,]    177     52 16.59804
 [5,]    178     71 22.40879
 [6,]    172     69 23.32342
 [7,]    165     61 22.40588
 [8,]    171     61 20.86112
 [9,]    178     51 16.09645
[10,]    170     75 25.95156
# Is the new object a matrix?
is.matrix(matrix)
[1] TRUE

Matrices

matrix <- cbind(height, weight, bmi)
matrix
      height weight      bmi
 [1,]    168     88 31.17914
 [2,]    177     72 22.98190
 [3,]    177     85 27.13141
 [4,]    177     52 16.59804
 [5,]    178     71 22.40879
 [6,]    172     69 23.32342
 [7,]    165     61 22.40588
 [8,]    171     61 20.86112
 [9,]    178     51 16.09645
[10,]    170     75 25.95156
# Is the new object a matrix?
is.matrix(matrix)
[1] TRUE
# How many dimensions does the matrix have?

Matrices

matrix <- cbind(height, weight, bmi)
matrix
      height weight      bmi
 [1,]    168     88 31.17914
 [2,]    177     72 22.98190
 [3,]    177     85 27.13141
 [4,]    177     52 16.59804
 [5,]    178     71 22.40879
 [6,]    172     69 23.32342
 [7,]    165     61 22.40588
 [8,]    171     61 20.86112
 [9,]    178     51 16.09645
[10,]    170     75 25.95156
# Is the new object a matrix?
is.matrix(matrix)
[1] TRUE
# How many dimensions does the matrix have?
dim(matrix)
[1] 10  3