L1: Operations and Objects in R

Quarto Notebooks, Operations and Objects

Bogdan G. Popescu

John Cabot University

What You’ll Learn Today

  1. Getting Started with Quarto and R
  2. Basic R Operations and Syntax
  3. Understanding Objects in R: numeric, character, logical objects
  4. Working with Data Structures: vectors, matrices, dataframes
  5. Exploring and Cleaning Data

By the end, you’ll be able to write simple R scripts and explore your own data.

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:

Using R

Let us now use R to understand how it works.

Let’s create a new quarto document and work with it.

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

Quarto files have the *.qmd extension

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:

Let us save the quarto file in a work folder called “example”.

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Press CMD + A or Ctrl + A and then Press Delete

Using R

Using R

Then type:

---
title: "Notebook"
author: "Your Name"
date: "July 26, 2025"
format:
  html:
    toc: true
    number-sections: true
    colorlinks: true
    smooth-scroll: true
    embed-resources: true
---

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

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

  • 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.

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
# This is a comment
# print a number
2
[1] 2

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

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 memory.

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: 5 is simply printed in the console.

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

Using R

This is what the output looks like in your working directory.

Numeric and string objects

# store a numeric object
x <- 2 

Numeric and string objects

Numeric and string objects

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 
# 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

Numeric and string objects

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

Spot the Difference

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

Spot the Difference

Spot the Difference

Spot the Difference

# Addition
addition <- 5+3

# Subtraction
subtraction <- 4-5

# Multiplication
multiplication <- 2*3

# Division
division <- 3/3

# Exponent
exponent <- 5^2

Spot the Difference

Spot the Difference

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
2 > 2
[1] FALSE
2 != 2
[1] FALSE

or

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

Conditional Operators Examples

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

Vectors of length 1

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

Vectors of length 1

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

# Storing a numeric vector
vector1 <- 600

Vectors of length 1

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

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

Vectors of length 1

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

Vectors of length 1

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

Vectors of length 1

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

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

Vectors of length 1

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

# Storing another character vector

Vectors of length 1

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

# Storing another character vector
vector2 <- "Hello"

Vectors of length 1

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

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

Vectors of length 1

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

Vectors of length 1

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

Vectors of length 1

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

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

Vectors of length 1

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

# Storing another logical vector
vector3 <- TRUE

Vectors of length 1

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

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

Vectors of length 1

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

Vectors of length 1

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

Vectors of length 1

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 with various inputs

Vectors

A vector of length > 1 can be achieved by using the c function with 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 with various inputs

For example we can create a vector of length 2

# Storing a vector of length 2

The c function

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

For example we can create a vector of length 2

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

The c function

A vector of length > 1 can be achieved by using the c function with 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

The c function

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

The c function

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

The c function

A vector of length > 1 can be achieved by using the c function with 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

Vector Naming Conventions in R

  • Vector names should be descriptive:
    ages, income_levels, country_codes
  • Use snake_case or camelCase, but be consistent:
    • average_income (snake_case)
    • averageIncome (camelCase)
  • Avoid starting names with numbers or special characters:
    2nd_vector, #income
  • Avoid reserved words or function names:
    mean, sum, data
  • R is case-sensitive: Incomeincome

Vectors of different types

We can also create a vector made out of different elements

Vectors of different types

We can also create a vector made out of different elements

# Storing a vector of elements of different types

Vectors of different types

We can also create a vector made out of different elements

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

Vectors of different types

We can also create a vector made out of different elements

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

Vectors of different types

We can also create a vector made out of different elements

Vectors of different types

We can also create a vector made out of different elements

Vectors of character elements

We can also create a vector (list) made out of character elements

Vectors of character elements

We can also create a vector (list) made out of character elements

#This is a list
list_fields <- c("politics", "philosophy", "literature", "chemistry")

Vectors of character elements

Vectors of character elements

Vectors of character elements

This is how we remove an element from the list

#This is a list
list_fields <- c("politics", "philosophy", "literature", "chemistry")

This is how we remove strings from vectors

#This is how we get rid of the element called "politics"
list_fields_new <- list_fields[list_fields != "politics"]

Vectors of character elements

Notice the length difference

length(list_fields)
[1] 4
length(list_fields_new)
[1] 3

Logical Subsetting

This is how we can do logical subsetting

# Create a numeric vector
list_no <- c(11, 12, 13, 14, 15, 16, 17)

Logical Subsetting

This is how we can do logical subsetting

# Create a numeric vector
list_no <- c(11, 12, 13, 14, 15, 16, 17)

# Keep values less than or equal to 13 OR greater than or equal to 15
list_no_filtered <- list_no[list_no <= 13 | list_no >= 15]

Logical Subsetting

This is how we can do logical subsetting

# Create a numeric vector
list_no <- c(11, 12, 13, 14, 15, 16, 17)

# Keep values less than or equal to 13 OR greater than or equal to 15
list_no_filtered <- list_no[list_no <= 13 | list_no >= 15]
list_no_filtered
[1] 11 12 13 15 16 17

Logical Subsetting

This is how we can do logical subsetting

# Create a numeric vector
list_no <- c(11, 12, 13, 14, 15, 16, 17)

# Keep values less than or equal to 13 OR greater than or equal to 15
list_no_filtered <- list_no[list_no <= 13 | list_no >= 15]

Logical Subsetting

This is how we can do logical subsetting

# Create a numeric vector
list_no <- c(11, 12, 13, 14, 15, 16, 17)

# Keep values less than or equal to 13 OR greater than or equal to 15
list_no_filtered <- list_no[list_no <= 13 | list_no >= 15]

# Printing the second element
list_no[2]
[1] 12

Logical Subsetting

This is how we can do logical subsetting

# Create a numeric vector
list_no <- c(11, 12, 13, 14, 15, 16, 17)

# Keep values less than or equal to 13 OR greater than or equal to 15
list_no_filtered <- list_no[list_no <= 13 | list_no >= 15]

# Printing the second, the 3rd, the 4th and 5th component
list_no[2:5]
[1] 12 13 14 15

Logical Subsetting

This is how we can do logical subsetting with strings

# Create a character vector
list_words <- c("random", "word", "sentence", "books")
# List of words to exclude
exclusion_list <- c("word", "sentence", "books")
# Keep only elements not in the exclusion list
list_words_filtered <- list_words[!(list_words %in% exclusion_list)]
list_words_filtered
[1] "random"

Strings & Comparison

This is how we can check which string is “greater” alphabetically and how to count the number of characters in a string

# Check which string is "greater" alphabetically
"four" > "five"  # TRUE because "o" comes after "i" in the alphabet
[1] TRUE
# Count the number of characters in the word
nchar("four")
[1] 4

Missing Data

This is how we can handle missing data

# Create a vector with a missing value (NA)
list_no <- c(1, 2, 3, 4, NA, 5, 6)

Missing Data

This is how we can handle missing data

# Create a vector with a missing value (NA)
list_no <- c(1, 2, 3, 4, NA, 5, 6)
# Checking if there is NA
is.na(list_no)
[1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE

Missing Data

This is how we can handle missing data

# Create a vector with a missing value (NA)
list_no <- c(1, 2, 3, 4, NA, 5, 6)
# Checking if there is NA
is.na(list_no)
[1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
# Remove missing values using !is.na()
list_no_clean <- list_no[!is.na(list_no)]

Missing Data

This is how we can handle missing data

# Create a vector with a missing value (NA)
list_no <- c(1, 2, 3, 4, NA, 5, 6)
# Checking if there is NA
is.na(list_no)
[1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
# Remove missing values using !is.na()
list_no_clean <- list_no[!is.na(list_no)]
list_no_clean
[1] 1 2 3 4 5 6

Mean & Median

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

# Create a numeric vector with missing values
list_no <- c(30, 12, NA, 14, NA)
# Compute sum while ignoring NAs
sum(list_no, na.rm = TRUE)
[1] 56
# Compute mean while ignoring NAs
mean(list_no, na.rm = TRUE)
[1] 18.66667
# Compute median while ignoring NAs
median(list_no, na.rm = TRUE)
[1] 14

Basic Operations with Vectors

Here is how we can perform basic mathematical operations with vectors

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

Basic Operations with Vectors

Here is how we can perform basic mathematical operations with vectors

# This is another vector 
weight <- c(88, 72, 85, 52, 71, 69, 61, 61, 51, 75)
# This is another vector 
height <- c(168, 177, 177, 177, 178, 172, 165, 171, 178, 170)

Basic Operations with Vectors

Here is how we can perform basic mathematical operations with vectors

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

Basic Operations with Vectors

Here is how we can perform basic mathematical operations with vectors

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

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 parameters.

The seq function

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

  • from - where to start

The seq function

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

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

The seq function

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

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

The seq function

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

  • 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 parameters.

  • 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 parameters.

  • 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
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

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 - simply pastes with no separator.

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

Defining vectors:

# 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)

Creating the matrix:

matrix <- cbind(height, weight, bmi)

Matrices

Visualizing the matrix:

print(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

Matrices

Matrices

Visualizing the matrix:

print(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

Visualizing the matrix:

print(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

How many dimensions does the matrix have?

dim(matrix)
[1] 10  3

Matrices

Matrices

Matrices

Dataframes

  • The matrix we just created can be turned into a dataframe.
  • Dataframes are essentially lists of vectors with names

Dataframes

  • The matrix we just created can be turned into a dataframe.

  • Dataframes are essentially lists of vectors with names

# First install the packages
install.packages("tibble")

You should see something like

trying URL 'https://cran.rstudio.com/bin/macosx/big-sur-arm64/contrib/4.5/tibble_3.3.0.tgz'
Content type 'application/x-gzip' length 692985 bytes (676 KB)
==================================================
downloaded 676 KB

The downloaded binary packages are in
    /var/folders/vl/wq4b0z_s3mj_rvz59myqgmx40000gn/T//RtmpdWk47v/downloaded_packages

Dataframes

  • The matrix we just created can be turned into a dataframe.

  • Dataframes are essentially lists of vectors with names

  • Once you are done comment out the install command

# First install the packages
#install.packages("tibble")

Dataframes

  • The matrix we just created can be turned into a dataframe.

  • Dataframes are essentially lists of vectors with names

#Load the tidyverse tibble package
library(tibble)
mydat <- as_tibble(matrix)
mydat
# A tibble: 10 × 3
   height weight   bmi
    <dbl>  <dbl> <dbl>
 1    168     88  31.2
 2    177     72  23.0
 3    177     85  27.1
 4    177     52  16.6
 5    178     71  22.4
 6    172     69  23.3
 7    165     61  22.4
 8    171     61  20.9
 9    178     51  16.1
10    170     75  26.0

Dataframes

  • The matrix we just created can be turned into a dataframe.

  • Dataframes are essentially lists of vectors with names

Dataframes

  • The matrix we just created can be turned into a dataframe.

  • Dataframes are essentially lists of vectors with names

Dataframes

  • The matrix we just created can be turned into a dataframe.

  • Dataframes are essentially lists of vectors with names

#Load the tidyverse tibble package
library(tibble)
mydat <- as_tibble(matrix)
#Seeing the column names
names(mydat)
[1] "height" "weight" "bmi"   

Dataframes

Dataframes

Dataframes

Dataframes

Dataframes

Dataframes

Within a dataframe:

  • Each table row represents an observation, with values possibly of a different type for each variable
  • Each table column represents a variable, with values of the same type

Dataframes

Dataframes

Dataframes

Dataframes

We can also create a dataframe manually in the following way:

# This is another vector
mydat<-data.frame(
  weight = c(88, 72, 85, 52, 71, 69, 61, 61, 51, 75),
  height = c(168, 177, 177, 177, 178, 172, 165, 171, 178, 170)
)
mydat
   weight height
1      88    168
2      72    177
3      85    177
4      52    177
5      71    178
6      69    172
7      61    165
8      61    171
9      51    178
10     75    170

Dataframes

We can also create a dataframe manually in the following way:

# 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)
mydat<-data.frame(weight, height)
mydat
   weight height
1      88    168
2      72    177
3      85    177
4      52    177
5      71    178
6      69    172
7      61    165
8      61    171
9      51    178
10     75    170

Dataframe properties

Some important dataframe properties include:

  • nrow - number of rows
  • ncol - number of columns
  • dim - both the number of rows and columns
  • rownames - reveals the index numbers of the dataframe
  • colnames - reveals the column names

Dataframe properties

Here is how they would work

Dataframe properties

Here is how they would work

# This is how we count the number of rows
nrow(mydat)
[1] 10

Dataframe properties

Dataframe properties

Here is how they would work

# This is how we count the number of rows
nrow(mydat)
[1] 10
# This is how we count the number of columns
ncol(mydat)
[1] 2

Dataframe properties

Dataframe properties

Here is how they would work

# This is how we count the number of rows
nrow(mydat)
[1] 10
# This is how we count the number of columns
ncol(mydat)
[1] 2
# This is how we count both the number of rows and number of columns
dim(mydat)
[1] 10  2

Dataframe properties

Dataframe properties

Here is how they would work

# This is how we count the number of rows
nrow(mydat)
[1] 10
# This is how we count the number of columns
ncol(mydat)
[1] 2
# This is how we count both the number of rows and number of columns
dim(mydat)
[1] 10  2
# This is how we identify index names
rownames(mydat)
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"

Dataframe properties

Dataframe properties

Here is how they would work

# This is how we count the number of rows
nrow(mydat)
[1] 10
# This is how we count the number of columns
ncol(mydat)
[1] 2
# This is how we count both the number of rows and number of columns
dim(mydat)
[1] 10  2
# This is how we identify index names
rownames(mydat)
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"
# This is how we identify column/variable names
colnames(mydat)
[1] "weight" "height"

Dataframe properties

Dataframe properties

These properties allow us to also make changes to the dataframe.

For example, we can change column names:

colnames(mydat)[colnames(mydat)=="weight"]<-"body_weight"
colnames(mydat)
[1] "body_weight" "height"     

Dataframe properties

Dataframe properties

Dataframe properties

The glimpse command from dplyr allows us to see the dataframe effectively.

library(dplyr)
glimpse(mydat)
Rows: 10
Columns: 2
$ body_weight <dbl> 88, 72, 85, 52, 71, 69, 61, 61, 51, 75
$ height      <dbl> 168, 177, 177, 177, 178, 172, 165, 171, 178, 170

The $ operator is a shortcut for getting a single column, by name, from a data.frame:

Example:

mydat$height
 [1] 168 177 177 177 178 172 165 171 178 170

Dataframe properties

head and tail allow us to see the beginning and the end of our dataframe

For example, the following command gives us the first 4 entries

head(mydat, 4)
  body_weight height
1          88    168
2          72    177
3          85    177
4          52    177

The following command gives us the last 4 entries

tail(mydat, 4)
   body_weight height
7           61    165
8           61    171
9           51    178
10          75    170

What Have We Learned?

  • Used Quarto for combining code and narrative
  • Performed basic arithmetic and assignments in R
  • Worked with object types: numeric, character, logical
  • Created and manipulated vectors, matrices, and data frames
  • Handled missing data with is.na() and na.rm
  • Used essential functions like mean(), paste(), sort(), rep(), seq()

You now have the foundation to write reproducible scripts and explore real data in R.