Question 1

data(cars)

median(cars$speed)
## [1] 15

The median car speed is 15

Question 2

# install.packages("jsonlite")
library(jsonlite)
btc_data <- fromJSON("https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&limit=100")
btc_df <- btc_data$Data$Data
max_close <-max(btc_df$close)

The max daily close is 123374.56

Question 3 - Additional Research Questions (3A, 3B, 3C)

View data summary

str(df1)
## Classes 'tbl_df', 'tbl' and 'data.frame':    83 obs. of  5 variables:
##  $ city        : chr  "Baltimore" "Chicago" "Houston" "Cleveland" ...
##  $ state       : chr  "Maryland" "Illinois" "Texas" "Ohio" ...
##  $ murders_2014: int  211 411 242 63 105 90 248 78 41 159 ...
##  $ murders_2015: int  344 478 303 120 162 145 280 109 72 188 ...
##  $ change      : int  133 67 61 57 57 55 32 31 31 29 ...
##  - attr(*, "spec")=List of 2
##   ..$ cols   :List of 5
##   .. ..$ city        : list()
##   .. .. ..- attr(*, "class")= chr [1:2] "collector_character" "collector"
##   .. ..$ state       : list()
##   .. .. ..- attr(*, "class")= chr [1:2] "collector_character" "collector"
##   .. ..$ 2014_murders: list()
##   .. .. ..- attr(*, "class")= chr [1:2] "collector_integer" "collector"
##   .. ..$ 2015_murders: list()
##   .. .. ..- attr(*, "class")= chr [1:2] "collector_integer" "collector"
##   .. ..$ change      : list()
##   .. .. ..- attr(*, "class")= chr [1:2] "collector_integer" "collector"
##   ..$ default: list()
##   .. ..- attr(*, "class")= chr [1:2] "collector_guess" "collector"
##   ..- attr(*, "class")= chr "col_spec"
summary(df1)
##      city              state            murders_2014     murders_2015   
##  Length:83          Length:83          Min.   :  0.00   Min.   :  1.00  
##  Class :character   Class :character   1st Qu.: 19.50   1st Qu.: 22.50  
##  Mode  :character   Mode  :character   Median : 32.00   Median : 39.00  
##                                        Mean   : 65.75   Mean   : 75.48  
##                                        3rd Qu.: 82.00   3rd Qu.: 94.00  
##                                        Max.   :411.00   Max.   :478.00  
##      change       
##  Min.   :-19.000  
##  1st Qu.: -3.000  
##  Median :  4.000  
##  Mean   :  9.735  
##  3rd Qu.: 14.000  
##  Max.   :133.000
head(df1)
##         city     state murders_2014 murders_2015 change
## 1  Baltimore  Maryland          211          344    133
## 2    Chicago  Illinois          411          478     67
## 3    Houston     Texas          242          303     61
## 4  Cleveland      Ohio           63          120     57
## 5 Washington      D.C.          105          162     57
## 6  Milwaukee Wisconsin           90          145     55

Clean and Rename

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
df_clean <- df1 %>%
  rename(
    CITY = city,
    STATE = state,
    MURDERS_2014 = murders_2014,
    MURDERS_2015 = murders_2015,
    CHANGE = change
  )

QUESTION 3A:Which US States have the highest percentage increase in murder rates from 2014 to 2015 ?

df_clean2 <- df_clean %>%
  mutate(
    PERCENT_CHANGE = ((MURDERS_2015 - MURDERS_2014) / MURDERS_2014) * 100,
    PERCENT_CHANGE = ifelse(is.infinite(PERCENT_CHANGE), NA, PERCENT_CHANGE) 
  ) 
  top10_df <- df_clean2 %>%
  filter(!is.na(PERCENT_CHANGE)) %>%
  arrange(desc(PERCENT_CHANGE)) %>%
  head(10)

# ANSWER: Aurora Colorado and Anchorage Alaska have the highest percentage increase. 
library(ggplot2)

ggplot(top10_df, aes(x = reorder(CITY, PERCENT_CHANGE), y = PERCENT_CHANGE, fill = STATE)) +
  geom_col() +
  coord_flip() +
  labs(
    title = "Top 10 U.S. Cities by Percent Increase in Murders (2014–2015)",
    x = "City",
    y = "Percent Change in Murders",
    fill = "State"
  )

QUESTION 3B: What is the total number of murders across all cities in 2014 vs. 2015, and what is the overall percent change?

TOTAL_2014 <- sum(df_clean$MURDERS_2014, na.rm = TRUE)
TOTAL_2015 <- sum(df_clean$MURDERS_2015, na.rm = TRUE)
OVERALL_PERCENT_CHANGE <- ((TOTAL_2015 - TOTAL_2014) / TOTAL_2014) * 100

TOTAL_2014
## [1] 5457
TOTAL_2015
## [1] 6265
OVERALL_PERCENT_CHANGE
## [1] 14.80667
# The total number of murders across all cities in the US in 2014 was 5457 and in 2015 there were 6265 murders, showing a 14.80667% INCREASE in the number of murders across all cities.

QUESTION 3C: What is the median percent change in murders, and how does it compare to the mean percent change?

MEAN_PERCENT <- mean(df_clean2$PERCENT_CHANGE, na.rm = TRUE)
MEDIAN_PERCENT <- median(df_clean2$PERCENT_CHANGE, na.rm = TRUE)

MEDIAN_PERCENT
## [1] 9.666667
MEAN_PERCENT
## [1] 15.75206
# The median percent change in murders is ~9.67% and the mean percent change is ~15.75%. This shows that there are probably a few cities who are skewing the results.