getwd()
## [1] "/cloud/project"

Question 5

Calculate the field goal percentage for each game and select the correct average field goal percentage for the five games.

The average field goal percentage is 46.9%.

# Field goals made
FGM <- c(18, 7, 6, 9, 10, 13)

# Field goals attempted
FGA <- c(36, 23, 12, 18, 24, 22)

# Calculate field goal percentage
FG_percent <- (FGM / FGA) * 100

# Display each game's FG percentage
FG_percent
## [1] 50.00000 30.43478 50.00000 50.00000 41.66667 59.09091
# Calculate average field goal percentage
average_FG_percent <- mean(FG_percent)

# Display average
average_FG_percent
## [1] 46.86539

Question 6

Calculate both the average of the individual per-game three-point percentages and the overall (cumulative) three-point shooting percentage across all seven games.

Average individual game 3-point percentage: 40.51%

Overall cumulative 3-point percentage: 42.17%

# Three-pointers made
threePM <- c(3, 5, 10, 6, 3, 7, 1)

# Three-pointers attempted
threePA <- c(9, 10, 18, 12, 11, 12, 11)

# Calculate individual game three-point percentages
three_point_percentage <- (threePM / threePA) * 100

# Display each game's percentage
three_point_percentage
## [1] 33.333333 50.000000 55.555556 50.000000 27.272727 58.333333  9.090909
# Calculate average of individual game percentages
average_individual_percentage <- mean(three_point_percentage)

# Display average individual percentage
average_individual_percentage
## [1] 40.51227
# Calculate cumulative three-point percentage
total_threePM <- sum(threePM)
total_threePA <- sum(threePA)

overall_percentage <- (total_threePM / total_threePA) * 100

# Display overall percentage
overall_percentage
## [1] 42.16867

Question 7

Compute the On-Base Percentage (OBP) for each player and select the player with the highest OBP. Submit your code and knitted R HTML file or RPubs link as instructed.

(Formula: OBP = (Hits + Walks) / (At-Bats + Walks))

The highest OBP is:

Player 4 has the highest On-Base Percentage (OBP) at approximately 0.3885 (38.85%).

# Create the baseball dataset

PlayerID <- c(1, 2, 3, 4, 5)
Hits <- c(112, 124, 121, 106, 140)
At_Bats <- c(400, 450, 380, 500, 402)
HR <- c(25, 22, 8, 20, 11)
BB <- c(50, 60, 19, 150, 55)
SO <- c(60, 65, 67, 92, 70)

# Calculate On-Base Percentage (OBP)
OBP <- (Hits + BB) / (At_Bats + BB)

# Display OBP for each player
OBP
## [1] 0.3600000 0.3607843 0.3508772 0.3938462 0.4266958
# Create a table showing Player ID and OBP
OBP_table <- data.frame(PlayerID, OBP)

OBP_table
##   PlayerID       OBP
## 1        1 0.3600000
## 2        2 0.3607843
## 3        3 0.3508772
## 4        4 0.3938462
## 5        5 0.4266958
# Find player with highest OBP
highest_OBP_player <- OBP_table[which.max(OBP_table$OBP), ]

highest_OBP_player
##   PlayerID       OBP
## 5        5 0.4266958