Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution by Yue for Jun Gi #337

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
106 changes: 106 additions & 0 deletions Projects/JungiHong/Yue_Z9.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
title: "Yue_Z9"
output: md_document
date: "2024-12-04"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r pressure,echo=FALSE}

library(ggplot2)
library(tidyverse)
library(readxl)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be more simple to load the meta package library(tidyverse) instead of individual subpackages


#the first one
#read xlsx file
file<-"Volleyball Passing- USA and TU.xlsx"
data <- read_excel(file,sheet = "TU sort by pass score")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • best set the working directory to the source file's location before calling read_excel() to ensure the file is found.

new_data <- data[2:5, 6:9]

#Formation of new data
colnames(new_data) <- c("Pass Score", "Attempts", "Points_Won", "Points_Lost")

clean_data <- new_data %>%
mutate(
Pass_Score = as.factor("Pass Score"),
Attempts = as.numeric(Attempts),
Points_Won = as.numeric(Points_Won),
Points_Lost = as.numeric(Points_Lost)
) %>%
pivot_longer(
cols = c(Points_Won, Points_Lost),
names_to = "Outcome",
values_to = "Count"
) %>%
group_by(`Pass Score`) %>%
mutate(`Pass Score` = factor(`Pass Score`, levels = c("zeros", "ones", "twos", "threes")))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can omit line 39 since factoring with predefined levels would make values that dont fit an NA..


# visualization
ggplot(clean_data, aes(x = `Pass Score`, y= Count ,fill = Outcome)) +
geom_bar(stat = "identity", position = "stack") +
labs(
title = "Stacked Bar Chart: Breakdown of Points Won and Lost by Pass Score",
x = "Pass Score Categories",
y = "Total Attempts"
) +
theme_minimal() +
scale_fill_manual(values = c("Points_Won" = "skyblue", "Points_Lost" = "tomato"))

#THE second
#get data
folder_path <- "passing stats"
file_list <- list.files(path = folder_path, pattern = "\\.xlsx$", full.names = TRUE)
#read every data
extract_totals_data <- function(file_path) {
data <- read_excel(file_path, sheet = "totals")
#last 3
last_three_rows <- tail(data, 3)
colnames(last_three_rows) <- as.character(last_three_rows[1, ])
#delate first row
last_three_rows <- last_three_rows[-1, ]
#filter
filtered_data <- last_three_rows[, c("side-outs", "points", "average")]
return(filtered_data)
}
#new data
results_list <- lapply(file_list, extract_totals_data)
final_results <- do.call(rbind, results_list)
final_results <- final_results %>%
mutate(average = round(as.numeric(average), 2))
print(final_results)

final_results$points <- as.numeric(as.character(final_results$points))
final_results$average <- as.numeric(as.character(final_results$average))

#won and lost
won_data <- final_results[final_results$`side-outs` == "won", ]
lost_data <- final_results[final_results$`side-outs` == "lost", ]


# cor
won_cor <- cor(won_data$average, won_data$points, use = "complete.obs")
lost_cor <- cor(lost_data$average,lost_data$points, use = "complete.obs")

# plot
ggplot(final_results, aes(x = average, y = points, color = `side-outs`, shape = `side-outs`)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE) +
scale_color_manual(values = c("won" = "blue", "lost" = "red")) +
scale_shape_manual(values = c("won" = 16, "lost" = 17)) +
labs(title = "Correlation Between Passing Scores and Points Won/Lost",
x = "Passing Score (Average)",
y = "Points (Won or Lost)",
color = "Side-outs", shape = "Side-outs") +
annotate("text", x = 2.0, y = 180,
label = paste("Won Trend (r =", round(won_cor, 2), ")", sep = ""),
color = "blue", hjust = 0, vjust = 1) +
annotate("text", x = 2.0, y = 170,
label = paste("Lost Trend (r =", round(lost_cor, 2), ")", sep = ""),
color = "red", hjust = 0, vjust = 1) +
theme_minimal() +
theme(legend.position = "topleft")


```
39 changes: 39 additions & 0 deletions Projects/JungiHong/Yue_Z9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── 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
## New names:

![](Yue_Z9_files/figure-markdown_strict/pressure-1.png)

## New names:
## New names:
## New names:
## New names:
## New names:
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`

## # A tibble: 10 × 3
## `side-outs` points average
## <chr> <chr> <dbl>
## 1 won 150 2.83
## 2 lost 53 2.04
## 3 won 117 2.66
## 4 lost 30 2.14
## 5 won 140 2.69
## 6 lost 81 2.08
## 7 won 161 2.52
## 8 lost 58 2.52
## 9 won 93 2.38
## 10 lost 67 2.09

## `geom_smooth()` using formula = 'y ~ x'

![](Yue_Z9_files/figure-markdown_strict/pressure-2.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.