## Importing Candy Data

```{r}
candy_file <- "candy-data.csv"
candy <- read.csv(candy_file, row.names = 1)
head(candy)
```

> Q1. How many different candy types are in this dataset?


```{r}
nrow(candy)
```

> Q2. How many fruity candy types are in the dataset?

```{r}
sum(candy$fruity)
```

```{r}
candy["Twix", ]$winpercent
```

```{r}
library(dplyr)
```

```{r}
candy |>
filter(row.names(candy)=="Twix") |>
select(winpercent)
```

> Q3. What is your favorite candy (other than Twix) in the dataset and what is it’s winpercent value?

```{r}
candy["Snickers", ]$winpercent
```

> Q4. What is the winpercent value for “Kit Kat”?

```{r}
candy["Kit Kat", ]$winpercent
```

> Q5. What is the winpercent value for “Tootsie Roll Snack Bars”?

```{r}
candy["Tootsie Roll Snack Bars", ]$winpercent
```

```{r}
library("skimr")
skim(candy)
```

> Q6. Is there any variable/column that looks to be on a different scale to the majority of the other columns in the dataset?

Winpercent

> Q7. What do you think a zero and one represent for the candy$chocolate column?

A zero indicates that it does not contain chocolate, while a one indicates that it does contain chocolate.


## Exploratory Analysis

> Q8. Plot a histogram of winpercent values

```{r}
hist(candy$winpercent)
```

> Q9. Is the distribution of winpercent values symmetrical?

No, they are not symmetrical.

> Q10. Is the center of the distribution above or below 50%?

```{r}
mean(candy$winpercent)
```

Above 50%

Q11. On average is chocolate candy higher or lower ranked than fruit candy?
1. Find all chocolate candy
2. Get their winpercent values
3. Find the mean
4. Find all fruit candy
5. Get their winpercent values
6. Find the mean
7. Compare the two means


```{r}
choc.candy <- candy[candy$chocolate == 1, ]
choc.win <- choc.candy$winpercent
mean(choc.win)
```

```{r}
fruit.win <- candy[candy$fruity == 1,]$winpercent
mean(fruit.win)
```

> Q12. Is this difference statistically significant?

```{r}
t.test(choc.win, fruit.win)
```

There is a statistical difference.

> Q13. What are the five least liked candy types in this set?

```{r}
y <- c("y", "a", "z")
sort(y)
```

```{r}
y
```

```{r}
order(y)
```

```{r}
ord.ind <- order(candy$winpercent)
candy[ord.ind,]
```

> Q14. What are the top 5 all time favorite candy types out of this set?

```{r}
head(candy[order(candy$winpercent, decreasing = TRUE), ])

```

> Q15. Make a first barplot of candy ranking based on winpercent values.


We need a custom color vector

```{r}
my_cols <- rep("black", nrow(candy))
my_cols[candy$chocolate==1] <- "chocolate"
my_cols[candy$fruity==1] <- "red"
my_cols
```

## Taking a look at Pricepercent

```{r}
library(ggrepel)
```

Loading required package: ggplot2

```{r}
ggplot(candy) +
aes(x=pricepercent, y=winpercent, label=rownames(candy)) +
geom_point(col=my_cols) +
geom_text_repel(col=my_cols, size=3.3, max.overlaps = 50)
```

> Q19. Which candy type is the highest ranked in terms of winpercent for the least money - i.e. offers the most bang for your buck?

Reese’s Peanut Butter Cup Miniatures.

> Q20. What are the top 5 most expensive candy types in the dataset and of these which is the least popular?

```{r}
candy |> arrange(desc(pricepercent)) |> head(5)
```

## Exploring the correlation structure

```{r}
cij <- cor(candy)
```


```{r}
library(corrplot)
```

```{r}
corrplot(cij)
```

> Q22. Examining this plot what two variables are anti-correlated (i.e. have minus values)?

Chocolate and fruity are anti-correlated.

> Q23. Similarly, what two variables are most positively correlated?

Chocolate and bar are most positively correlated.

## Principal Component Analysis (PCA)

```{r}
pca <- prcomp(candy, scale=TRUE)
summary(pca)
```

Score plot…

```{r}
plot(pca$x[,1], pca$x[,2],
xlab = "PC1",
ylab = "PC2",
main = "PCA of Candy Dataset")
```

```{r}
plot(pca$x[,1:2], col = my_cols, pch = 16)
```

```{r}
ggplot(pca$rotation) +
aes(PC1, row.names(pca$rotation)) +
geom_col()
```

> Q24. Complete the code to generate the loadings plot above. What original variables are picked up strongly by PC1 in the positive direction? Do these make sense to you? Where did you see this relationship highlighted previously?

```{r}
ggplot(pca$rotation) +
  aes(PC1, PC2) +
  geom_point()
```

PC1 mainly separates chocolate‑based candies from fruity/non‑chocolate ones.Variables like chocolate, bar, peanutyalmondy, caramel, and nougat load strongly positive on PC1, while fruity, hard, and pluribus load strongly negative. This matches what we saw earlier in the correlation matrix and exploratory plots: chocolate candies cluster together and tend to be more similar to each other than to fruity candies.

> Q25. Based on your exploratory analysis, correlation findings, and PCA results, what combination of characteristics appears to make a “winning” candy? How do these different analyses (visualization, correlation, PCA) support or complement each other in reaching this conclusion?

Across the exploratory plots, correlation matrix, and PCA, the same pattern appears: the most successful candies are chocolate‑based, often bars, and frequently include peanuts, almonds, caramel, or nougat. These traits cluster together in the correlation heatmap, load strongly in the positive direction on PC1, and dominate the high‑winpercent end of the barplots. Fruity or hard candies consistently fall on the opposite side of PC1 and tend to have lower winpercent values. Together, these analyses show that chocolate + bar structure + nut/caramel components make a “winning” candy.







