L3: Matrices, Dataframes, If/Else Statements

Bogdan G. Popescu

John Cabot University

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

Dataframes

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

Dataframes

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

  • Dataframes are essentially, list of vectors with names

Dataframes

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

  • Dataframes are essentially, list of vectors with names

#Load the tidyverse tibble package
library(tibble)

Dataframes

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

  • Dataframes are essentially, list of vectors with names

#Load the tidyverse tibble package
library(tibble)
mydat <- as_tibble(matrix)

Dataframes

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

  • Dataframes are essentially, list 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, list 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
names(mydat)
[1] "height" "weight" "bmi"   

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

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

Interactive view of a dataframe

We view our dataframe(s) by clicking on the environment

Interactive view of a dataframe

We view our dataframe(s) by clicking on the environment

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

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

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

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

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

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

For example, we can change column names:

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

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

Conditionals

The purpose of a conditional is execution of code

An if / else condition in R contains the following components

  • The if keyword
  • Condition inside parantheses
  • Code to be executed if the condition is TRUE, inside curly brackets ({ and })
  • The else keyword (optional)
  • Code to be executed if the condition is FALSE, inside curly brackets ({ and }) (optional)

Conditionals

  • The conditional is evaluated to a logical vector containing either TRUE or FALSE if the condition is TRUE

  • If the condition is TRUE, the code after if is executed

  • If the condition is FALSE, the code after else is executed

if(condition) {
  expression
}

Conditionals

Here is type of syntax without else

if(condition) {
  expression
}

Here is type of syntax with else

if(condition) {
  expression
} else {
  expression
}

Conditionals Examples

Let us look at some examples:

x = 3

Conditionals Examples

Let us look at some examples:

x = 3
if(x > 0)

Conditionals Examples

Let us look at some examples:

x = 3
if(x > 0) {
  print('x is positive!')
}

Conditionals Examples

Let us look at some examples:

x = 3
if(x > 0) {
  print('x is positive!')
}
[1] "x is positive!"

Conditionals Examples

What happens if the condition is false?

Conditionals Examples

What happens if the condition is false?

x = -5

Conditionals Examples

What happens if the condition is false?

x = -5
if(x > 0)

Conditionals Examples

What happens if the condition is false?

x = -5
if(x > 0) {
  print('x is positive!')
}

Conditionals Examples

What happens if the condition is false?

x = -5
if(x > 0) {
  print('x is positive!')
}

Conditionals Examples

What happens if the condition is false?

x = -5
if(x > 0) {
  print('x is positive!')
}

Nothing happens

Conditionals Examples

To have something printed, we would have have to add the else condition

Conditionals Examples

To have something printed, we would have have to add the else condition

x = -5
if(x > 0) {
  print('x is positive!')
}

Conditionals Examples

To have something printed, we would have have to add the else condition

x = -5
if(x > 0) {
  print('x is positive!')
}else{
  print('x is negative or zero!')
}

Conditionals Examples

To have something printed, we would have have to add the else condition

x = -5
if(x > 0) {
  print('x is positive!')
}else{
  print('x is negative or zero!')
}
[1] "x is negative or zero!"

Conditionals Examples

To have something printed, we would have have to add the else condition

x = -5
if(x > 0) {
  print('x is positive!')
}else{
  print('x is negative or zero!')
}
[1] "x is negative or zero!"

When the condition is FALSE, the second code section is executed instead.

Complex Conditionals

We can have more than two conditions.

We should however create a function for that.

Exercise 1

Objectives Write an R script that classifies a given number into one of three categories:

  • Positive: If the number is greater than 0
  • Negative: If the number is less than 0
  • Zero: If the number is exactly 0

Let’s say the number is 3.

Instructions Use if-else statements to classify the number into one of the categories. The output should look like below:

number<-3
[1] "The number is Positive"
[1] "The number is Positive."

Exercise 2

Objective Write an R script that classifies a person into different age groups based on the following criteria:

  • Child: 0 to 12 years old
  • Teenager: 13 to 19 years old
  • Adult: 20 to 59 years old
  • Senior: 60 years old and above

Let’s say the age is is 15.

Instructions

Use if-else statements to classify the age into one of the age groups.

age<-15
[1] "You are a Teenager."

Exercise 3

Objective Write an R script that classifies temperatures into different categories based on the following criteria:

  • Cold: Less than 50 degrees Fahrenheit
  • Moderate: 50 to 75 degrees Fahrenheit
  • Warm: 76 to 90 degrees Fahrenheit
  • Hot: More than 90 degrees Fahrenheit

Instructions 1. Use if-else statements to classify the temperature into one of the categories.

temperature<-50
[1] "The temperature is Moderate."