#Case-scenario 1 #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_Homeruns <- 20
# Number of seasons
n_seasons <- 4
#how to answer the question above
#20=(11+13+12+x/4)
#80-36=x
# Needed Home-runs on season 4
x_4 <- n_seasons*Wanted_Hoemruns - sum(HR_before)
# Minimum number of Home-runs needed by Robert
x_4
[1] 44
remove(Wanted_Hoemruns)
# 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
summary(Robert_HRs)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  11.00   11.75   12.50   20.00   20.75   44.00 

Question 1 This is the sixth season of outfielder Juan Soto in the majors. If during the first five seasons he received 79, 108,41,145, and 135 walks, how many does he need on this season for his overall number of walks per season to be at least 100?

Soto_Walks<-c(79,108,41,145,135)
wanted_walks<-100
number_seasons<-6
#Needed walks on season 6
walks_6<-number_seasons*wanted_walks-sum(Soto_Walks)
walks_6
[1] 92

Answer:Soto needs 92 walks in his sixth season

Case Scenario 2

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

Question 2 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.

bp_1<-7
fp_1<-9
w_1<-102000
w_2<-91000
#Mean Salary Overall
w_salary_ave<-(bp_1*w_1+fp_1*w_2)/(bp_1+fp_1)
w_salary_ave
[1] 95812.5

Answer:mean salary of all 16 professional players is $95,812.50

Case Scenario 3

getwd()
[1] "/cloud/project"
contract_length<-read.csv("allcontracts.csv",header=TRUE,sep=",")
contract_years<-contract_length$years
contracts_mean<-mean(contract_years,digits=3)
contracts_mean
[1] 3.46
#Median
contracts_median<-median(contract_years)
contracts_median
[1] 3
#SD
#Find the number of observations
contracts_n<-length(contract_years)
contracts_n
[1] 500
#Find the standard deviation
contracts_sd<-sd(contract_years)
contracts_sd
[1] 1.695331
contracts_w1sd<-sum((contract_years-contracts_mean)/contracts_sd<1)/contracts_n
#Percentage of observations within one sd from the mean
contracts_w1sd
[1] 0.842
#Difference from emperical
contracts_w1sd-.68
[1] 0.162
#Percentage of observations within two sd from the mean
contracts_w2sd<-sum((contract_years-contracts_mean)/contracts_sd<2)/contracts_n 
contracts_w2sd
[1] 1

So this means that 100% of the contracts are within 2 Standard Deviations

#Difference from emperical
contracts_w2sd-.95
[1] 0.05
#Within 3 SD
#Percentage of observations within three sd from the mean
contracts_w3sd<-sum((contract_years-contracts_mean)/contracts_sd<3)/contracts_n 
contracts_w3sd
[1] 1
#Difference from empirical
contracts_w2sd-.9973
[1] 0.0027

Create a Histogram

summary(cars)
     speed           dist       
 Min.   : 4.0   Min.   :  2.00  
 1st Qu.:12.0   1st Qu.: 26.00  
 Median :15.0   Median : 36.00  
 Mean   :15.4   Mean   : 42.98  
 3rd Qu.:19.0   3rd Qu.: 56.00  
 Max.   :25.0   Max.   :120.00  
hist(cars$dist)
#graph appears

Create a Histogram

hist(contract_years,xlab = "Years Left in Contract",col="green",border = "red",
     xlim = c(0,8),ylim = c(0,200),breaks =5)

#boxplot
boxplot(contract_years,main="Years left in Contract",ylab="Years")

#boxplot
boxplot(contract_years,main="Years left in Contract",ylab="Years",col="lightblue",
        border = "blue",horizontal = FALSE )

Question_3 Use the skills learned in case scenario number 3 on one the following data sets. You may choose only one dataset.

doubles<-read.table("doubles_hit.csv",header=TRUE,sep=",")
doubles_hit<-doubles$doubles_hit
doubles_hit_mean<-mean(doubles_hit)
doubles_hit_median<-median(doubles_hit)

doubles_hit_mean
[1] 23.55
doubles_hit_median
[1] 23.5
doubles_hit_n<-length(doubles_hit)
doubles_hit_sd<-sd(doubles_hit)

doubles_hit_n
[1] 100
doubles_hit_sd
[1] 13.37371
doubles_hit_w1sd<-sum((doubles_hit - doubles_hit_mean)/doubles_hit_sd<1)/doubles_hit_n
doubles_hit_w1sd
[1] 0.79
#Difference from empirical
doubles_hit_w1sd-.68
[1] 0.11
doubles_hit_w2sd<-sum((doubles_hit - doubles_hit_mean)/doubles_hit_sd<2)/doubles_hit_n
doubles_hit_w2sd
[1] 1
#Difference from empirical
doubles_hit_w2sd-.95
[1] 0.05
doubles_hit_w3sd<-sum((doubles_hit - doubles_hit_mean)/doubles_hit_sd<3)/doubles_hit_n
doubles_hit_w3sd
[1] 1
#Difference from empirical
doubles_hit_w2sd-.9973
[1] 0.0027

Double hits Histogram

hist(doubles_hit,xlab = "Number of Doubles",
     ylab="Years",col="lightblue",border = "blue",
     xlim=c(0,60),ylim=c(0,30),breaks=7
     )

#boxplot
boxplot(doubles_hit,main="Boxplot of Double Hits by Player",ylab="Doubles",
        col="lightblue",border = "blue" )

LS0tCnRpdGxlOiAiR2V0dGluZyBTdGFydGVkIHdpdGggUjpDb25jZXB0ZXMgaW4gU3BvcnRzIEFuYWx5dGljcyBwYXJ0IDIiCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KI0Nhc2Utc2NlbmFyaW8gMQojVGhpcyBpcyB0aGUgZm91cnRoIHNlYXNvbiBvZiBvdXRmaWVsZGVyIEx1aXMgUm9iZXJ0IHdpdGggI3RoZSBDaGljYWdvIFdoaXRlIFNvY2tzLiBJZiBkdXJpbmcgdGhlIGZpcnN0IHRocmVlICNzZWFzb25zIGhlIGhpdCAxMSwgMTMsIGFuZCAxMiBob21lIHJ1bnMsIGhvdyBtYW55IGRvZXMgaGUgI25lZWQgb24gdGhpcyBzZWFzb24gZm9yIGhpcyBvdmVyYWxsIGF2ZXJhZ2UgdG8gYm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