library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ 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(ggplot2)
library(dbplyr)
## 
## Attaching package: 'dbplyr'
## 
## The following objects are masked from 'package:dplyr':
## 
##     ident, sql
dataset <- read.csv("baseball.csv",header = TRUE)
#A numeric summary of data for at least 10 columns of data
dataset%>%
  summarize(AVG_runscore=mean(RS), AVG_runsgiven=mean(RA),Top_team_wins= quantile(W,.75),Top_team_OBP=quantile(OBP,.75),Top_team_SLG = quantile(SLG,.75), Top_team_BA = quantile(BA,.75), Top_team_OOBP = max(OOBP,na.rm = TRUE), Top_team_OSLG = max(SLG,na.rm = TRUE), Total_seasons = max(Year)- min(Year)+1)
##   AVG_runscore AVG_runsgiven Top_team_wins Top_team_OBP Top_team_SLG
## 1      715.082       715.082            89        0.337        0.421
##   Top_team_BA Top_team_OOBP Top_team_OSLG Total_seasons
## 1       0.268         0.384         0.491            51
Total_teams = unique(dataset[,c("Team")])
#A set of at least 5 novel questions to investigate informed by the following:
#Question1: Because of AL is using designated hitter and the NL's pitcher has to bat, I want to see is their any difference in scoring and hitting. 
#Question2: I want to figure out which stats is most relevant to winning games and playoffs
#Question3: I want to find out is their any statistical thing that is in common between every champion. 
#Question4: I will use the stats from the top 25% in AVG, SLG, OBP to see how deep they go into the playoffs and compare it to see which stats is most important for a playoff run. 
#Question5: Last I want to see how MLB's playing style has changed throughout the years based on their offense performance. 
#Use of aggregation functions (other than the ones used from the first bullet, above)
dataset %>%
  group_by(RankPlayoffs,League)%>%
  summarise(total_wins=n())
## `summarise()` has grouped output by 'RankPlayoffs'. You can override using the
## `.groups` argument.
## # A tibble: 12 × 3
## # Groups:   RankPlayoffs [6]
##    RankPlayoffs League total_wins
##           <int> <chr>       <int>
##  1            1 AL             25
##  2            1 NL             22
##  3            2 AL             22
##  4            2 NL             25
##  5            3 AL             40
##  6            3 NL             40
##  7            4 AL             34
##  8            4 NL             34
##  9            5 AL              1
## 10            5 NL              1
## 11           NA AL            494
## 12           NA NL            494
# A visual summary of at least 5 columns of your data #1
avg_by_year <- dataset %>%
  group_by(Year)%>%
  summarise(avg=mean(BA))

ggplot(avg_by_year)+
  geom_line(mapping=aes(x=Year,y=avg))

# A visual summary of at least 5 columns of your data #2
total_teams_year <- dataset %>%
  group_by(Year)%>%
  summarise(teams=n())

ggplot(total_teams_year)+
  geom_line(mapping=aes(x=Year,y=teams))

# A visual summary of at least 5 columns of your data #3
moneyball_year <- dataset %>%
  group_by(Year)%>%
  summarise(OBP=mean(OBP),RS=mean(RS)/162)

ggplot(moneyball_year,aes(x=RS,y=OBP))+geom_point()

# A visual summary of at least 5 columns of your data #4
redsox_stats <- dataset%>%
  filter(Team=="BOS")

ggplot(redsox_stats)+
  geom_point(mapping=aes(x=Year,y=RS, color=RankSeason))+
  geom_line(mapping=aes(x=Year,y=RS),color="black")

ggplot(redsox_stats)+
  geom_point(mapping=aes(x=W,y=RS,color="RS"))+
  geom_point(mapping=aes(x=W,y=RA,color="RA"))