COVID-19 Data Analysis
Load libraries
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
Load data
# Example if your file is CSV
canada_covid19_dataset <- read.csv("C:/Users/ranjo/OneDrive/Desktop/Rclass/canada_covid19_dataset.csv", stringsAsFactors = FALSE)
Structure of the dataset
str(canada_covid19_dataset)
## 'data.frame': 3630 obs. of 23 variables:
## $ pruid : int 59 48 47 46 35 24 10 13 12 11 ...
## $ prname : chr "British Columbia" "Alberta" "Saskatchewan" "Manitoba" ...
## $ prnameFR : chr "Colombie-Britannique" "Alberta" "Saskatchewan" "Manitoba" ...
## $ date : chr "08-02-2020" "08-02-2020" "08-02-2020" "08-02-2020" ...
## $ reporting_week : int 6 6 6 6 6 6 6 6 6 6 ...
## $ reporting_year : int 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 ...
## $ update : num 1 1 1 1 1 1 1 1 1 1 ...
## $ totalcases : chr "4" "0" "0" "0" ...
## $ numtotal_last7 : chr "3" "0" "0" "0" ...
## $ ratecases_total : chr "0.07" "0" "0" "0" ...
## $ numdeaths : int 0 0 0 0 0 0 0 0 0 0 ...
## $ numdeaths_last7 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ ratedeaths : num 0 0 0 0 0 0 0 0 0 0 ...
## $ ratecases_last7 : chr "0.05" "0" "0" "0" ...
## $ ratedeaths_last7 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ numtotal_last14 : chr "4" "0" "0" "0" ...
## $ numdeaths_last14 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ ratetotal_last14 : chr "0.07" "0" "0" "0" ...
## $ ratedeaths_last14 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ avgcases_last7 : chr "0.43" "0" "0" "0" ...
## $ avgincidence_last7 : chr "0.01" "0" "0" "0" ...
## $ avgdeaths_last7 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ avgratedeaths_last7: num 0 0 0 0 0 0 0 0 0 0 ...
List the variables in your dataset
names(canada_covid19_dataset)
## [1] "pruid" "prname" "prnameFR"
## [4] "date" "reporting_week" "reporting_year"
## [7] "update" "totalcases" "numtotal_last7"
## [10] "ratecases_total" "numdeaths" "numdeaths_last7"
## [13] "ratedeaths" "ratecases_last7" "ratedeaths_last7"
## [16] "numtotal_last14" "numdeaths_last14" "ratetotal_last14"
## [19] "ratedeaths_last14" "avgcases_last7" "avgincidence_last7"
## [22] "avgdeaths_last7" "avgratedeaths_last7"
Print the top 15 rows of your dataset
head(canada_covid19_dataset, n=15)
## pruid prname prnameFR date
## 1 59 British Columbia Colombie-Britannique 08-02-2020
## 2 48 Alberta Alberta 08-02-2020
## 3 47 Saskatchewan Saskatchewan 08-02-2020
## 4 46 Manitoba Manitoba 08-02-2020
## 5 35 Ontario Ontario 08-02-2020
## 6 24 Quebec Québec 08-02-2020
## 7 10 Newfoundland and Labrador Terre-Neuve-et-Labrador 08-02-2020
## 8 13 New Brunswick Nouveau-Brunswick 08-02-2020
## 9 12 Nova Scotia Nouvelle-Écosse 08-02-2020
## 10 11 Prince Edward Island Île-du-Prince-Édouard 08-02-2020
## 11 60 Yukon Yukon 08-02-2020
## 12 61 Northwest Territories Territoires du Nord-Ouest 08-02-2020
## 13 62 Nunavut Nunavut 08-02-2020
## 14 99 Repatriated travellers Voyageurs rapatriés 08-02-2020
## 15 1 Canada Canada 08-02-2020
## reporting_week reporting_year update totalcases numtotal_last7
## 1 6 2020 1 4 3
## 2 6 2020 1 0 0
## 3 6 2020 1 0 0
## 4 6 2020 1 0 0
## 5 6 2020 1 4 1
## 6 6 2020 1 0 0
## 7 6 2020 1 0 0
## 8 6 2020 1 0 0
## 9 6 2020 1 0 0
## 10 6 2020 1 0 0
## 11 6 2020 1 0 0
## 12 6 2020 1 0 0
## 13 6 2020 1 0 0
## 14 6 2020 NA 0 0
## 15 6 2020 NA 8 4
## ratecases_total numdeaths numdeaths_last7 ratedeaths ratecases_last7
## 1 0.07 0 0 0 0.05
## 2 0 0 0 0 0
## 3 0 0 0 0 0
## 4 0 0 0 0 0
## 5 0.03 0 0 0 0.01
## 6 0 0 0 0 0
## 7 0 0 0 0 0
## 8 0 0 0 0 0
## 9 0 0 0 0 0
## 10 0 0 0 0 0
## 11 0 0 0 0 0
## 12 0 0 0 0 0
## 13 0 0 0 0 0
## 14 0 0 NA
## 15 0.02 0 0 0 0.01
## ratedeaths_last7 numtotal_last14 numdeaths_last14 ratetotal_last14
## 1 0 4 0 0.07
## 2 0 0 0 0
## 3 0 0 0 0
## 4 0 0 0 0
## 5 0 1 0 0.01
## 6 0 0 0 0
## 7 0 0 0 0
## 8 0 0 0 0
## 9 0 0 0 0
## 10 0 0 0 0
## 11 0 0 0 0
## 12 0 0 0 0
## 13 0 0 0 0
## 14 NA 0 0
## 15 0 5 0 0.01
## ratedeaths_last14 avgcases_last7 avgincidence_last7 avgdeaths_last7
## 1 0 0.43 0.01 0
## 2 0 0 0 0
## 3 0 0 0 0
## 4 0 0 0 0
## 5 0 0.14 0 0
## 6 0 0 0 0
## 7 0 0 0 0
## 8 0 0 0 0
## 9 0 0 0 0
## 10 0 0 0 0
## 11 0 0 0 0
## 12 0 0 0 0
## 13 0 0 0 0
## 14 NA 0 0
## 15 0 0.57 0 0
## avgratedeaths_last7
## 1 0
## 2 0
## 3 0
## 4 0
## 5 0
## 6 0
## 7 0
## 8 0
## 9 0
## 10 0
## 11 0
## 12 0
## 13 0
## 14 NA
## 15 0
Write a user defined function using any of the variables from the
data set
avgDeathsPerCase <- function(dataset) {
#Select the last available row for Canada that has a value for totalcases and numdeaths
last_available_data <- dataset %>%
filter(prname == "Canada" & totalcases != "-" & numdeaths != "-") %>%
select(totalcases, numdeaths) %>%
tail(1)
#Extract values and assign to a variable
casetotal <- as.numeric(last_available_data$totalcases)
deathtotal <- as.numeric(last_available_data$numdeaths)
#compute average deaths per case
average <- deathtotal / casetotal
return(average)
}
avgDeathsPerCase(canada_covid19_dataset)
## [1] 0.01201389
Use data manipulation techniques and filter rows based on any
logical criteria that exist in your dataset.
ontario_data <- canada_covid19_dataset %>% filter(prname == "Ontario")
head(ontario_data)
## pruid prname prnameFR date reporting_week reporting_year update
## 1 35 Ontario Ontario 08-02-2020 6 2020 1
## 2 35 Ontario Ontario 15-02-2020 7 2020 1
## 3 35 Ontario Ontario 22-02-2020 8 2020 1
## 4 35 Ontario Ontario 29-02-2020 9 2020 1
## 5 35 Ontario Ontario 07-03-2020 10 2020 1
## 6 35 Ontario Ontario 14-03-2020 11 2020 1
## totalcases numtotal_last7 ratecases_total numdeaths numdeaths_last7
## 1 4 1 0.03 0 0
## 2 4 0 0.03 0 0
## 3 5 1 0.03 0 0
## 4 18 13 0.12 0 0
## 5 33 15 0.21 0 0
## 6 181 148 1.16 1 1
## ratedeaths ratecases_last7 ratedeaths_last7 numtotal_last14 numdeaths_last14
## 1 0.00 0.01 0.00 1 0
## 2 0.00 0 0.00 1 0
## 3 0.00 0.01 0.00 1 0
## 4 0.00 0.08 0.00 14 0
## 5 0.00 0.1 0.00 28 0
## 6 0.01 0.95 0.01 163 1
## ratetotal_last14 ratedeaths_last14 avgcases_last7 avgincidence_last7
## 1 0.01 0.00 0.14 0
## 2 0.01 0.00 0 0
## 3 0.01 0.00 0.14 0
## 4 0.09 0.00 1.86 0.01
## 5 0.18 0.00 2.14 0.01
## 6 1.04 0.01 21.14 0.14
## avgdeaths_last7 avgratedeaths_last7
## 1 0.00 0
## 2 0.00 0
## 3 0.00 0
## 4 0.00 0
## 5 0.00 0
## 6 0.14 0
Identify the dependent & independent variables and use reshaping
techniques and create a new data frame by joining those variables from
your dataset.
independent_var <- canada_covid19_dataset[, c("prname", "date", "reporting_year")]
dependent_var <- canada_covid19_dataset[, "numdeaths", drop = FALSE]
reshaped_data <- cbind(independent_var, dependent_var)
head(reshaped_data)
## prname date reporting_year numdeaths
## 1 British Columbia 08-02-2020 2020 0
## 2 Alberta 08-02-2020 2020 0
## 3 Saskatchewan 08-02-2020 2020 0
## 4 Manitoba 08-02-2020 2020 0
## 5 Ontario 08-02-2020 2020 0
## 6 Quebec 08-02-2020 2020 0
Remove missing values in your dataset.
remove_missing_values_data <- na.omit(canada_covid19_dataset)
Identify and remove duplicated data in your dataset
duplicated_rows <- remove_missing_values_data[duplicated(remove_missing_values_data), ]
print(duplicated_rows)
## [1] pruid prname prnameFR
## [4] date reporting_week reporting_year
## [7] update totalcases numtotal_last7
## [10] ratecases_total numdeaths numdeaths_last7
## [13] ratedeaths ratecases_last7 ratedeaths_last7
## [16] numtotal_last14 numdeaths_last14 ratetotal_last14
## [19] ratedeaths_last14 avgcases_last7 avgincidence_last7
## [22] avgdeaths_last7 avgratedeaths_last7
## <0 rows> (or 0-length row.names)
no_duplicate_data <- remove_missing_values_data %>% distinct()
Reorder multiple rows in descending order
cleansed_data <- no_duplicate_data %>% arrange(desc(date))
head(cleansed_data)
## pruid prname prnameFR date reporting_week
## 1 59 British Columbia Colombie-Britannique 31-12-2022 52
## 2 48 Alberta Alberta 31-12-2022 52
## 3 47 Saskatchewan Saskatchewan 31-12-2022 52
## 4 46 Manitoba Manitoba 31-12-2022 52
## 5 35 Ontario Ontario 31-12-2022 52
## 6 24 Quebec Québec 31-12-2022 52
## reporting_year update totalcases numtotal_last7 ratecases_total numdeaths
## 1 2022 1 393145 692 7123.47 4896
## 2 2022 1 623991 870 13289.72 5421
## 3 2022 1 151570 302 12535.7 1822
## 4 2022 1 153784 134 10570.06 2369
## 5 2022 1 1550579 6635 9934.28 16189
## 6 2022 1 1285181 5987 14481.43 17314
## numdeaths_last7 ratedeaths ratecases_last7 ratedeaths_last7 numtotal_last14
## 1 13 88.71 12.54 0.24 1248
## 2 26 115.46 18.53 0.55 1693
## 3 15 150.69 24.98 1.24 628
## 4 0 162.83 9.21 0.00 277
## 5 71 103.72 42.51 0.45 13129
## 6 109 195.09 67.46 1.23 13103
## numdeaths_last14 ratetotal_last14 ratedeaths_last14 avgcases_last7
## 1 90 22.61 1.63 98.86
## 2 55 36.06 1.17 124.29
## 3 22 51.94 1.82 43.14
## 4 38 19.04 2.61 19.14
## 5 158 84.12 1.01 947.86
## 6 218 147.64 2.46 855.29
## avgincidence_last7 avgdeaths_last7 avgratedeaths_last7
## 1 1.79 1.86 0.03
## 2 2.65 3.71 0.08
## 3 3.57 2.14 0.18
## 4 1.32 0.00 0.00
## 5 6.07 10.14 0.06
## 6 9.64 15.57 0.18
Rename some of the column names in your dataset
renamed_data <- cleansed_data %>%
rename(
Province = prname,
Province_French = prnameFR,
Total_Cases = totalcases,
Total_Deaths = numdeaths
)
head(renamed_data)
## pruid Province Province_French date reporting_week
## 1 59 British Columbia Colombie-Britannique 31-12-2022 52
## 2 48 Alberta Alberta 31-12-2022 52
## 3 47 Saskatchewan Saskatchewan 31-12-2022 52
## 4 46 Manitoba Manitoba 31-12-2022 52
## 5 35 Ontario Ontario 31-12-2022 52
## 6 24 Quebec Québec 31-12-2022 52
## reporting_year update Total_Cases numtotal_last7 ratecases_total Total_Deaths
## 1 2022 1 393145 692 7123.47 4896
## 2 2022 1 623991 870 13289.72 5421
## 3 2022 1 151570 302 12535.7 1822
## 4 2022 1 153784 134 10570.06 2369
## 5 2022 1 1550579 6635 9934.28 16189
## 6 2022 1 1285181 5987 14481.43 17314
## numdeaths_last7 ratedeaths ratecases_last7 ratedeaths_last7 numtotal_last14
## 1 13 88.71 12.54 0.24 1248
## 2 26 115.46 18.53 0.55 1693
## 3 15 150.69 24.98 1.24 628
## 4 0 162.83 9.21 0.00 277
## 5 71 103.72 42.51 0.45 13129
## 6 109 195.09 67.46 1.23 13103
## numdeaths_last14 ratetotal_last14 ratedeaths_last14 avgcases_last7
## 1 90 22.61 1.63 98.86
## 2 55 36.06 1.17 124.29
## 3 22 51.94 1.82 43.14
## 4 38 19.04 2.61 19.14
## 5 158 84.12 1.01 947.86
## 6 218 147.64 2.46 855.29
## avgincidence_last7 avgdeaths_last7 avgratedeaths_last7
## 1 1.79 1.86 0.03
## 2 2.65 3.71 0.08
## 3 3.57 2.14 0.18
## 4 1.32 0.00 0.00
## 5 6.07 10.14 0.06
## 6 9.64 15.57 0.18
Add new variables in your data frame by using a mathematical
function
new_data <- renamed_data %>%
mutate(
Total_Cases = as.numeric(ifelse(Total_Cases == "-", NA, Total_Cases)),
Total_Deaths = as.numeric(ifelse(Total_Deaths == "-", NA, Total_Deaths)),
DeathsPerCase = Total_Deaths / Total_Cases,
PercentDeaths = (Total_Deaths / Total_Cases) * 100
)
head(new_data)
## pruid Province Province_French date reporting_week
## 1 59 British Columbia Colombie-Britannique 31-12-2022 52
## 2 48 Alberta Alberta 31-12-2022 52
## 3 47 Saskatchewan Saskatchewan 31-12-2022 52
## 4 46 Manitoba Manitoba 31-12-2022 52
## 5 35 Ontario Ontario 31-12-2022 52
## 6 24 Quebec Québec 31-12-2022 52
## reporting_year update Total_Cases numtotal_last7 ratecases_total Total_Deaths
## 1 2022 1 393145 692 7123.47 4896
## 2 2022 1 623991 870 13289.72 5421
## 3 2022 1 151570 302 12535.7 1822
## 4 2022 1 153784 134 10570.06 2369
## 5 2022 1 1550579 6635 9934.28 16189
## 6 2022 1 1285181 5987 14481.43 17314
## numdeaths_last7 ratedeaths ratecases_last7 ratedeaths_last7 numtotal_last14
## 1 13 88.71 12.54 0.24 1248
## 2 26 115.46 18.53 0.55 1693
## 3 15 150.69 24.98 1.24 628
## 4 0 162.83 9.21 0.00 277
## 5 71 103.72 42.51 0.45 13129
## 6 109 195.09 67.46 1.23 13103
## numdeaths_last14 ratetotal_last14 ratedeaths_last14 avgcases_last7
## 1 90 22.61 1.63 98.86
## 2 55 36.06 1.17 124.29
## 3 22 51.94 1.82 43.14
## 4 38 19.04 2.61 19.14
## 5 158 84.12 1.01 947.86
## 6 218 147.64 2.46 855.29
## avgincidence_last7 avgdeaths_last7 avgratedeaths_last7 DeathsPerCase
## 1 1.79 1.86 0.03 0.012453420
## 2 2.65 3.71 0.08 0.008687625
## 3 3.57 2.14 0.18 0.012020848
## 4 1.32 0.00 0.00 0.015404724
## 5 6.07 10.14 0.06 0.010440616
## 6 9.64 15.57 0.18 0.013472032
## PercentDeaths
## 1 1.2453420
## 2 0.8687625
## 3 1.2020848
## 4 1.5404724
## 5 1.0440616
## 6 1.3472032
Create a training set using random number generator engine
set.seed(1234)
training_set <- new_data %>% sample_frac(0.75, replace = FALSE)
head(training_set)
## pruid Province Province_French date
## 1 10 Newfoundland and Labrador Terre-Neuve-et-Labrador 19-09-2020
## 2 60 Yukon Yukon 24-04-2021
## 3 35 Ontario Ontario 20-04-2024
## 4 59 British Columbia Colombie-Britannique 26-09-2020
## 5 24 Quebec Québec 06-03-2021
## 6 11 Prince Edward Island Île-du-Prince-Édouard 12-06-2021
## reporting_week reporting_year update Total_Cases numtotal_last7
## 1 38 2020 1 272 1
## 2 16 2021 1 80 4
## 3 16 2024 1 1714520 653
## 4 39 2020 1 8641 799
## 5 9 2021 1 284471 4952
## 6 23 2021 1 206 1
## ratecases_total Total_Deaths numdeaths_last7 ratedeaths ratecases_last7
## 1 50.5 3 0 0.56 0.19
## 2 177.88 1 0 2.22 8.89
## 3 10984.62 18627 14 119.34 4.18
## 4 156.57 230 7 4.17 14.48
## 5 3205.42 9925 67 111.83 55.8
## 6 118.54 0 0 0.00 0.58
## ratedeaths_last7 numtotal_last14 numdeaths_last14 ratetotal_last14
## 1 0.00 2 0 0.37
## 2 0.00 6 0 13.34
## 3 0.09 1365 25 8.75
## 4 0.13 1679 17 30.42
## 5 0.75 10334 139 116.44
## 6 0.00 4 0 2.3
## ratedeaths_last14 avgcases_last7 avgincidence_last7 avgdeaths_last7
## 1 0.00 0.14 0.03 0.00
## 2 0.00 0.57 1.27 0.05
## 3 0.16 93.29 0.6 2.00
## 4 0.31 114.14 2.07 1.00
## 5 1.57 707.43 7.97 9.57
## 6 0.00 0.12 0.07 0.00
## avgratedeaths_last7 DeathsPerCase PercentDeaths
## 1 0.00 0.01102941 1.102941
## 2 0.11 0.01250000 1.250000
## 3 0.01 0.01086427 1.086427
## 4 0.02 0.02661729 2.661729
## 5 0.11 0.03488932 3.488932
## 6 0.00 0.00000000 0.000000
Print the summary statistics of your dataset
summary(new_data)
## pruid Province Province_French date
## Min. :10.00 Length:2597 Length:2597 Length:2597
## 1st Qu.:13.00 Class :character Class :character Class :character
## Median :46.00 Mode :character Mode :character Mode :character
## Mean :35.77
## 3rd Qu.:48.00
## Max. :62.00
##
## reporting_week reporting_year update Total_Cases
## Min. : 1.00 Min. :2020 Min. :1 Min. : 0
## 1st Qu.:13.00 1st Qu.:2021 1st Qu.:1 1st Qu.: 1141
## Median :25.00 Median :2021 Median :1 Median : 56441
## Mean :25.86 Mean :2022 Mean :1 Mean : 246430
## 3rd Qu.:38.00 3rd Qu.:2023 3rd Qu.:1 3rd Qu.: 240220
## Max. :53.00 Max. :2024 Max. :1 Max. :1719315
## NA's :112
## numtotal_last7 ratecases_total Total_Deaths numdeaths_last7
## Length:2597 Length:2597 Min. : 0 Min. : -1.00
## Class :character Class :character 1st Qu.: 14 1st Qu.: 0.00
## Mode :character Mode :character Median : 586 Median : 4.00
## Mean : 3343 Mean : 23.34
## 3rd Qu.: 4311 3rd Qu.: 22.00
## Max. :20553 Max. :838.00
##
## ratedeaths ratecases_last7 ratedeaths_last7 numtotal_last14
## Min. : 0.00 Length:2597 Min. :-0.0800 Length:2597
## 1st Qu.: 4.31 Class :character 1st Qu.: 0.0000 Class :character
## Median : 50.32 Mode :character Median : 0.2400 Mode :character
## Mean : 63.53 Mean : 0.5628
## 3rd Qu.:111.50 3rd Qu.: 0.7400
## Max. :231.59 Max. :11.2600
##
## numdeaths_last14 ratetotal_last14 ratedeaths_last14 avgcases_last7
## Min. : -1.00 Length:2597 Min. :-0.080 Length:2597
## 1st Qu.: 0.00 Class :character 1st Qu.: 0.000 Class :character
## Median : 10.00 Mode :character Median : 0.560 Mode :character
## Mean : 46.61 Mean : 1.118
## 3rd Qu.: 45.00 3rd Qu.: 1.550
## Max. :1587.00 Max. :17.880
##
## avgincidence_last7 avgdeaths_last7 avgratedeaths_last7 DeathsPerCase
## Length:2597 Min. : -0.140 Min. :-0.01000 Min. :0.000000
## Class :character 1st Qu.: 0.000 1st Qu.: 0.00000 1st Qu.:0.006031
## Mode :character Median : 0.590 Median : 0.04000 Median :0.010673
## Mean : 3.335 Mean : 0.08036 Mean :0.013587
## 3rd Qu.: 3.140 3rd Qu.: 0.11000 3rd Qu.:0.014609
## Max. :119.710 Max. : 1.61000 Max. :0.142857
## NA's :202
## PercentDeaths
## Min. : 0.0000
## 1st Qu.: 0.6031
## Median : 1.0673
## Mean : 1.3587
## 3rd Qu.: 1.4609
## Max. :14.2857
## NA's :202
Plot a scatter plot for any 2 variables in your dataset
ontario_data <- new_data %>% filter(Province == "Ontario")
ggplot(ontario_data, aes(x = as.Date(date, format = "%d-%m-%Y"), y = Total_Cases)) +
geom_point(color = "blue") +
labs(
title = "COVID-19 Total Cases Over Time in Ontario",
x = "Date",
y = "Total Cases"
) +
theme_minimal()
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

Plot a bar plot for any 2 variables in your dataset
new_data <- new_data %>% mutate(date = as.Date(date, format = "%d-%m-%Y"))
latest_cases <- new_data %>% filter(!is.na(Total_Cases)) %>% group_by(Province) %>% filter(date == max(date)) %>% ungroup()
ggplot(latest_cases, aes(x = Province, y = Total_Cases)) +
geom_bar(stat = "identity", fill = "steelblue") +
labs(
title = "Latest Total COVID-19 Cases by Province",
x = "Province",
y = "Total Cases"
) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Find the correlation between any 2 variables by applying Pearson
correlation
cor_data <- new_data %>% filter(!is.na(Total_Cases) & !is.na(Total_Deaths))
correlation <- cor(cor_data$Total_Cases, cor_data$Total_Deaths, method = "pearson")
cat("Pearson correlation between Total Cases and Total Deaths:", correlation, "\n")
## Pearson correlation between Total Cases and Total Deaths: 0.9601831