This is the fourth season of outfielder Luis Robert with the Chicago White Socks. If during the first three seasons he hit 11, 13, and 12 home runs, how many does he need on this season for his overall average to be at least 20?

# Home-runs so far
HR_before <- c(11, 13, 12)
# Average Number of Home-runs per season wanted
wanted_HR <- 20
# Number of seasons
n_seasons <- 4
# Needed Home-runs on season 4
x_4 <- n_seasons*wanted_HR - sum(HR_before)
# Minimum number of Home-runs needed by Robert
x_4
## [1] 44

According to the calculations above, Robert must hit 44 home-runs or better on this season to get an average number of home-runs per season of at least 20.

# Robert's performance
Robert_HRs <- c(11, 13, 12,44)
# Find mean
mean(Robert_HRs)
## [1] 20
# Find standard deviation
sd(Robert_HRs)
## [1] 16.02082
# Find the maximum number of home-runs during the four seasons period
max(Robert_HRs)
## [1] 44
# Find the minimum number of home-runs during the four seasons period
min(Robert_HRs)
## [1] 11
summary(Robert_HRs)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   11.00   11.75   12.50   20.00   20.75   44.00

The average salary of 10 baseball players is 72,000 dollars a week and the average salary of 4 soccer players is 84,000. Find the mean salary of all 14 professional players.

n_1 <- 10
n_2 <- 4
y_1 <- 72000
y_2 <- 84000
# Mean salary overall
salary_ave <-  (n_1*y_1 + n_2*y_2)/(n_1+n_2)
salary_ave
## [1] 75428.57

The average salary of 7 basketball players is 102,000 dollars a week and the average salary of 9 NFL players is 91,000. Find the mean salary of all 16 professional players.

#walks so far 
walks_before <- c(79, 108,41,145,135)
#average number of walks per season wanted
wanted_walks <- 100
#number of seasons
n_seasons <- 6
#needed walks on season 6 
w_6<- n_seasons*wanted_walks-sum(walks_before)
#minimum number of walks needed by Juan Soto
w_6
## [1] 92

The average salary of 7 basketball players is 102,000 dollars a week and the average salary of 9 NFL players is 91,000. Find the mean salary of all 16 professional players.

n_1 <- 7
n_2 <- 9
y_1 <- 102000
y_2 <- 91000
# Mean salary overall
salary_ave <-  (n_1*y_1 + n_2*y_2)/(n_1+n_2)
salary_ave
## [1] 95812.5
getwd()
## [1] "/cloud/project"
contract_length <- read.table("allcontracts.csv", header = TRUE, sep = ",")
contract_years <- contract_length$years
# Mean 
contracts_mean  <- mean(contract_years)
contracts_mean
## [1] 3.458918
# Median
contracts_median <- median(contract_years)
contracts_median
## [1] 3
# Find number of observations
contracts_n <- length(contract_years)
# Find standard deviation
contracts_sd <- sd(contract_years)

What percentage of the data lies within one standard deviation of the mean

contracts_w1sd <- sum((contract_years - contracts_mean)/contracts_sd < 1)/ contracts_n
# Percentage of observation within one standard deviation of the mean
contracts_w1sd
## [1] 0.8416834
## Difference from empirical 
contracts_w1sd - 0.68
## [1] 0.1616834

What percentage of the data lies within two standard deviations of the mean?

## Within 2 sd
contracts_w2sd <- sum((contract_years - contracts_mean)/ contracts_sd < 2)/contracts_n
contracts_w2sd
## [1] 1
## Difference from empirical 
contracts_w2sd - 0.95
## [1] 0.05

What percent of the data lies within three standard deviations of the mean?

## Within 3 sd 
contracts_w3sd <- sum((contract_years - contracts_mean)/ contracts_sd < 3)/contracts_n
contracts_w3sd
## [1] 1
## Difference from empirical 
contracts_w3sd - 0.9973
## [1] 0.0027

Draw a histogram

# Create histogram
hist(contract_years,xlab = "Years Left in Contract",col = "green",border = "red", xlim = c(0,6), ylim = c(0,250),
   breaks = 3)

Answer to Question 3 Use the skills learned in case scenario number 3 on one the following data sets. You may choose only one dataset. They are both available in Canvas.

doubles <- read.table("doubles_hit.csv", header = TRUE, sep = ",")
doublesnumber <- doubles$doubles_hit
# Mean 
doublesnumber_mean  <- mean(doublesnumber)
doublesnumber_mean
## [1] 23.55
# Median
doublesnumber_median  <- median(doublesnumber)
doublesnumber_median
## [1] 23.5
# Find number of observations
players_n <- length(doublesnumber)
# Find standard deviation
players_sd <- sd(doublesnumber)
## Within 1 sd 
doublesnumber_w1sd <- sum((doublesnumber - doublesnumber_mean)/ players_sd < 1)/ players_n
# Percentage of observation within one standard deviation of the mean
doublesnumber_w1sd
## [1] 0.79
## Difference from empirical 
doublesnumber_w1sd - 0.68
## [1] 0.11
## Within 2 sd 
doublesnumber_w2sd <- sum((doublesnumber - doublesnumber_mean)/ players_sd < 2)/ players_n
# Percentage of observation within one standard deviation of the mean
doublesnumber_w2sd
## [1] 1
## Difference from empirical 
doublesnumber_w2sd - 0.95
## [1] 0.05
## Within 3 sd 
doublesnumber_w3sd <- sum((doublesnumber - doublesnumber_mean)/ players_sd < 3)/ players_n
# Percentage of observation within one standard deviation of the mean
doublesnumber_w3sd
## [1] 1
## Difference from empirical 
doublesnumber_w3sd - 0.9973
## [1] 0.0027
hist(doublesnumber,xlab = "Number of Doubles",col = "green",border = "red", xlim = c(0,4), ylim = c(0,100),
   breaks = 5)