1. Import File and summarize
library(readr)
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(magrittr)
library(curl)
## Using libcurl 7.64.1 with LibreSSL/2.8.3
## 
## Attaching package: 'curl'
## The following object is masked from 'package:readr':
## 
##     parse_date
#nfl_2007 <- read.csv("NFL2007Standings.csv")
nfl_2007<-read.csv(curl("https://raw.githubusercontent.com/brsingh7/R-Bridge/main/NFL2007Standings.csv?token=GHSAT0AAAAAABQQZHIYIQNP2JNGHSYBIUXQYO7L3AQ"))
summary(nfl_2007) #summarize data set
##        X             Team            Conference          Division        
##  Min.   : 1.00   Length:32          Length:32          Length:32         
##  1st Qu.: 8.75   Class :character   Class :character   Class :character  
##  Median :16.50   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :16.50                                                           
##  3rd Qu.:24.25                                                           
##  Max.   :32.00                                                           
##       Wins          Losses         WinPct         PointsFor     PointsAgainst  
##  Min.   : 1.0   Min.   : 0.0   Min.   :0.0630   Min.   :219.0   Min.   :262.0  
##  1st Qu.: 6.5   1st Qu.: 6.0   1st Qu.:0.4068   1st Qu.:273.2   1st Qu.:299.2  
##  Median : 8.0   Median : 8.0   Median :0.5000   Median :341.0   Median :349.5  
##  Mean   : 8.0   Mean   : 8.0   Mean   :0.5003   Mean   :347.0   Mean   :347.0  
##  3rd Qu.:10.0   3rd Qu.: 9.5   3rd Qu.:0.6250   3rd Qu.:395.2   3rd Qu.:385.8  
##  Max.   :16.0   Max.   :15.0   Max.   :1.0000   Max.   :589.0   Max.   :444.0  
##      NetPts            TDs       
##  Min.   :-175.0   Min.   :24.00  
##  1st Qu.: -99.0   1st Qu.:28.00  
##  Median :  -0.5   Median :37.50  
##  Mean   :   0.0   Mean   :38.84  
##  3rd Qu.:  73.5   3rd Qu.:46.25  
##  Max.   : 315.0   Max.   :75.00
#Group by division and summarize on average wins, points for and median points against
#sort desceneding on average wins
nfl_2007 %>% summarize(AvgWins=mean(Wins), AvgPts=mean(PointsFor), MedianAgainst=median(PointsAgainst)) %>% arrange(desc(AvgWins))
##   AvgWins AvgPts MedianAgainst
## 1       8    347         349.5
  1. Subset of the data. NFC teams with wins, losses, points for and tds
#NFC <- nfl_2007[c(Conference=="NFC"),c("Team", "Division", "Wins", "Losses", "PointsFor", "PointsAgainst", "TDs")]
NFC <- subset(nfl_2007, Conference == "NFC", select = c(Team, Division, Wins, Losses, PointsFor, PointsAgainst, TDs))
head(NFC, n=10)
##                    Team Division Wins Losses PointsFor PointsAgainst TDs
## 2        Dallas Cowboys      NCE   13      3       455           325  54
## 3     Green Bay Packers      NCN   13      3       435           291  49
## 8       New York Giants      NCE   10      6       373           351  44
## 10     Seattle Seahawks      NCW   10      6       393           291  44
## 12 Tampa Bay Buccaneers      NCS    9      7       334           270  36
## 13  Washington Redskins      NCE    9      7       334           310  35
## 14    Arizona Cardinals      NCW    8      8       404           399  49
## 16    Minnesota Vikings      NCN    8      8       365           311  43
## 17  Philadelphia Eagles      NCE    8      8       336           300  38
## 19    Carolina Panthers      NCS    7      9       267           347  28
  1. Rename Columns
colnames(NFC) <- c("Team_Name","NFC_Division", "W", "L", "Scored", "Against", "Touchdowns")
head(NFC,n=10)
##               Team_Name NFC_Division  W L Scored Against Touchdowns
## 2        Dallas Cowboys          NCE 13 3    455     325         54
## 3     Green Bay Packers          NCN 13 3    435     291         49
## 8       New York Giants          NCE 10 6    373     351         44
## 10     Seattle Seahawks          NCW 10 6    393     291         44
## 12 Tampa Bay Buccaneers          NCS  9 7    334     270         36
## 13  Washington Redskins          NCE  9 7    334     310         35
## 14    Arizona Cardinals          NCW  8 8    404     399         49
## 16    Minnesota Vikings          NCN  8 8    365     311         43
## 17  Philadelphia Eagles          NCE  8 8    336     300         38
## 19    Carolina Panthers          NCS  7 9    267     347         28
  1. Summarize subset, print mean and median for attributes in #1. Based on the subset, the average wins remained the same, as within each conference, that will not change because there is an evenly number of games played by each team (16 games in 2007). However, you do see that the average points scored (by 3.5) as well as the median of points against (by 2.0). This is because the data from the teams in the AFC is not included, and the data is telling us that the NFC, on average, scored less points in 2007.
summary(NFC) #summarize data set
##   Team_Name         NFC_Division             W               L        
##  Length:16          Length:16          Min.   : 3.00   Min.   : 3.00  
##  Class :character   Class :character   1st Qu.: 7.00   1st Qu.: 6.75  
##  Mode  :character   Mode  :character   Median : 8.00   Median : 8.00  
##                                        Mean   : 8.00   Mean   : 8.00  
##                                        3rd Qu.: 9.25   3rd Qu.: 9.00  
##                                        Max.   :13.00   Max.   :13.00  
##      Scored         Against        Touchdowns   
##  Min.   :219.0   Min.   :270.0   Min.   :24.00  
##  1st Qu.:317.2   1st Qu.:307.5   1st Qu.:32.50  
##  Median :341.0   Median :347.5   Median :37.50  
##  Mean   :343.5   Mean   :349.4   Mean   :38.44  
##  3rd Qu.:382.5   3rd Qu.:390.8   3rd Qu.:44.75  
##  Max.   :455.0   Max.   :444.0   Max.   :54.00
#Group by division and summarize on average wins, points for and median points against
#sort desceneding on average wins
NFC %>% summarize(AvgWins=mean(W), AvgPts=mean(Scored), MedianAgainst=median(Against)) %>% arrange(desc(AvgWins))
##   AvgWins AvgPts MedianAgainst
## 1       8  343.5         347.5
  1. Change values within columns
nfc_rename <- NFC
nfc_rename$NFC_Division[NFC$NFC_Division=="NCE"] <- "NFC East"
nfc_rename$NFC_Division[NFC$NFC_Division=="NCN"] <- "NFC North"
nfc_rename$NFC_Division[NFC$NFC_Division=="NCS"] <- "NFC South"
nfc_rename$NFC_Division[NFC$NFC_Division=="NCW"] <- "NFC West"
head(nfc_rename,n=10)
##               Team_Name NFC_Division  W L Scored Against Touchdowns
## 2        Dallas Cowboys     NFC East 13 3    455     325         54
## 3     Green Bay Packers    NFC North 13 3    435     291         49
## 8       New York Giants     NFC East 10 6    373     351         44
## 10     Seattle Seahawks     NFC West 10 6    393     291         44
## 12 Tampa Bay Buccaneers    NFC South  9 7    334     270         36
## 13  Washington Redskins     NFC East  9 7    334     310         35
## 14    Arizona Cardinals     NFC West  8 8    404     399         49
## 16    Minnesota Vikings    NFC North  8 8    365     311         43
## 17  Philadelphia Eagles     NFC East  8 8    336     300         38
## 19    Carolina Panthers    NFC South  7 9    267     347         28