# Loading in necessary libraries
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(GGally)Registered S3 method overwritten by 'GGally':
method from
+.gg ggplot2
library(ggfortify)
library(highcharter)Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
library(htmltools)
# Loading in data set
setwd("/Users/emiliodifilippantonio/Desktop/DATA 110/DATA 110 Working Directory")
guns <- read_csv("us_gun_deaths.csv")New names:
Rows: 389730 Columns: 21
── Column specification
──────────────────────────────────────────────────────── Delimiter: "," chr
(15): region, state, victim_age, victim_sex, victim_race, victim_race_pl... dbl
(5): ...1, year, month, multiple_victim_count, incident_id lgl (1):
additional_victim
ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
Specify the column types or set `show_col_types = FALSE` to quiet this message.
• `` -> `...1`
# Filtering for deaths that occured during or after 2000 CE
guns2 <- filter(guns, year >= 2000)
# Removing column one of the data set (index numbers)
guns3<- guns2[,-1]
# Converting numeric columns to numeric
guns3$year <- as.numeric(guns3$year)
guns3$month <- as.numeric(guns3$month)
guns3$victim_age <- as.numeric(guns3$victim_age)Warning: NAs introduced by coercion
guns3$multiple_victim_count <- as.numeric(guns3$multiple_victim_count)
guns3$incident_id <- as.numeric(guns3$incident_id)
# Converting factor columns to factor
guns3$region <- as.factor(guns3$region)
guns3$state <- as.factor(guns3$state)
guns3$victim_sex <- as.factor(guns3$victim_sex)
guns3$victim_race <- as.factor(guns3$victim_race)
guns3$victim_race_plus_hispanic <-
as.factor(guns3$victim_race_plus_hispanic)
guns3$victim_ethnicity <- as.factor(guns3$victim_ethnicity)
guns3$weapon_used <- as.factor(guns3$weapon_used)
guns3$victim_offender_split <- as.factor(guns3$victim_offender_split)
guns3$offenders_relationship_to_victim <-
as.factor(guns3$offenders_relationship_to_victim)
guns3$offenders_relationship_to_victim_grouping <-
as.factor(guns3$offenders_relationship_to_victim_grouping)
guns3$offender_sex <- as.factor(guns3$offender_sex)
guns3$circumstance <- as.factor(guns3$circumstance)
guns3$circumstance_grouping <- as.factor(guns3$circumstance_grouping)
guns3$extra_circumstance_info <- as.factor(guns3$extra_circumstance_info)
# Converting logical columns to logical
guns3$additional_victim <- as.logical(guns3$additional_victim)