Applied Computational Methods for Social Sciences
Lecture 3: Matrices, Dataframes, If/Else Statements
Bogdan G. Popescu
John Cabot University
What You’ll Learn Today
- Building and inspecting matrices
- Creating dataframes and exploring their properties
- Writing if/else conditionals
- Practicing with classification exercises
By the end, you’ll be able to organize data into tables and control the flow of your R scripts.
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
# Is the new object a matrix?
is.matrix(matrix)
# How many dimensions does the matrix have?
dim(matrix)
Dataframes
- The matrix we just created can be turned into a dataframe.
Dataframes
#Load the tidyverse tibble package
library(tibble)
Dataframes
#Load the tidyverse tibble package
library(tibble)
mydat <- as_tibble(matrix)
Dataframes
#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
#Seeing the column names
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:
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)
Dataframe Properties
Here is how they would work
# This is how we count the number of rows
nrow(mydat)
# This is how we count the number of columns
ncol(mydat)
Dataframe Properties
Here is how they would work
# This is how we count the number of rows
nrow(mydat)
# This is how we count the number of columns
ncol(mydat)
# This is how we count both the number of rows and number of columns
dim(mydat)
Dataframe Properties
Here is how they would work
# 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 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)
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
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
Dataframe Properties
The $ operator is a shortcut for getting a single column, by name, from a data.frame:
Example:
[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
body_weight height
1 88 168
2 72 177
3 85 177
4 52 177
Dataframe Properties
The following command gives us the last 4 entries
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
- Condition inside parentheses
- 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 the type of syntax without else
if(condition) {
expression
}
Here is the type of syntax with else
if(condition) {
expression
} else {
expression
}
Conditionals Examples
Let us look at some examples:
Conditionals Examples
Let us look at some examples:
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!')
}
Conditionals Examples
What happens if the condition is false?
Conditionals Examples
What happens if the condition is false?
Conditionals Examples
What happens if the condition is false?
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
To have something printed, we would have to add the else condition
Conditionals Examples
To have something printed, we would have to add the else condition
x = -5
if(x > 0) {
print('x is positive!')
}
Conditionals Examples
To have something printed, we would 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 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
Objective
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.
Exercise 1
Instructions
Use if-else statements to classify the number into one of the categories. The output should look like below:
[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 15.
Exercise 2
Instructions
Use if-else statements to classify the age into one of the age groups.
[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
Exercise 3
Instructions
Use if-else statements to classify the temperature into one of the categories.
[1] "The temperature is Moderate."
What Have We Learned?
- Built matrices with
cbind and inspected them with is.matrix and dim
- Turned matrices into dataframes and created them manually
- Explored dataframe properties:
nrow, ncol, dim, rownames, colnames, glimpse, $, head, tail
- Controlled program flow with if/else conditionals
- Practiced classifying numbers, ages, and temperatures
Next: functions and loops — automating repetitive work in R.