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(ggplot2)

# Load IPL matches dataset
matches <- read.csv("matches.csv")

# Filter matches where toss winner chose to field and won
field_wins <- matches %>%
  filter(toss_winner == winner & toss_decision == "field")

# Calculate the count and percentage
field_wins_count <- nrow(field_wins)
total_matches <- nrow(matches)
field_wins_percentage <- (field_wins_count / total_matches) * 100

# Create a pie chart
ggplot(data.frame(category = c("Fielding Wins", "Other"),
                  value = c(field_wins_count, total_matches - field_wins_count)),
       aes(x = "", y = value, fill = category)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  labs(title = "Toss Wins by Fielding Decision", fill = "Category") +
  theme_void() +
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_text(aes(label = paste(round(value/total_matches * 100), "%")), 
            position = position_stack(vjust = 0.5))

# Print the results
cat("Number of matches where toss-winning teams chose to field and won:", field_wins_count, "\n")
## Number of matches where toss-winning teams chose to field and won: 201
cat("Percentage:", field_wins_percentage, "%", "\n")
## Percentage: 31.60377 %