I have divided my work into two different parts the first part goes from question 1 to question 5 in the first part, I will focus on my data set and try to understand what the content of this data set is
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── 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
flavors_of_cacao <- read.csv("C:/Users/ASUS/Desktop/RESOURCE 4TH sem/r programming script/flavors_of_cacao.csv")
head(flavors_of_cacao)
## Company...Maker.if.known. Specific.Bean.Origin.or.Bar.Name REF Review.Date
## 1 A. Morin Agua Grande 1876 2016
## 2 A. Morin Kpime 1676 2015
## 3 A. Morin Atsane 1676 2015
## 4 A. Morin Akata 1680 2015
## 5 A. Morin Quilla 1704 2015
## 6 A. Morin Carenero 1315 2014
## Cocoa.Percent Company.Location Rating Bean.Type Broad.Bean.Origin
## 1 63% France 3.75 Sao Tome
## 2 70% France 2.75 Togo
## 3 70% France 3.00 Togo
## 4 70% France 3.50 Togo
## 5 70% France 3.50 Peru
## 6 70% France 2.75 Criollo Venezuela
dataset1 <- flavors_of_cacao
Q1 Before we start, we’ll understand how a dataset works by knowing the name of each variable.
glimpse(flavors_of_cacao)
## Rows: 1,795
## Columns: 9
## $ Company...Maker.if.known. <chr> "A. Morin", "A. Morin", "A. Morin", "…
## $ Specific.Bean.Origin.or.Bar.Name <chr> "Agua Grande", "Kpime", "Atsane", "Ak…
## $ REF <int> 1876, 1676, 1676, 1680, 1704, 1315, 1…
## $ Review.Date <int> 2016, 2015, 2015, 2015, 2015, 2014, 2…
## $ Cocoa.Percent <chr> "63%", "70%", "70%", "70%", "70%", "7…
## $ Company.Location <chr> "France", "France", "France", "France…
## $ Rating <dbl> 3.75, 2.75, 3.00, 3.50, 3.50, 2.75, 3…
## $ Bean.Type <chr> " ", " ", " ", " ", " ", "Criollo", "…
## $ Broad.Bean.Origin <chr> "Sao Tome", "Togo", "Togo", "Togo", "…
Now we’ll try to have a overview of the data in our dataset
#Q2
rename_dataset1 <-dataset1
summary(rename_dataset1)
## Company...Maker.if.known. Specific.Bean.Origin.or.Bar.Name REF
## Length:1795 Length:1795 Min. : 5
## Class :character Class :character 1st Qu.: 576
## Mode :character Mode :character Median :1069
## Mean :1036
## 3rd Qu.:1502
## Max. :1952
## Review.Date Cocoa.Percent Company.Location Rating
## Min. :2006 Length:1795 Length:1795 Min. :1.000
## 1st Qu.:2010 Class :character Class :character 1st Qu.:2.875
## Median :2013 Mode :character Mode :character Median :3.250
## Mean :2012 Mean :3.186
## 3rd Qu.:2015 3rd Qu.:3.500
## Max. :2017 Max. :5.000
## Bean.Type Broad.Bean.Origin
## Length:1795 Length:1795
## Class :character Class :character
## Mode :character Mode :character
##
##
##
From the summary, we can see some information : The review of chocolate data was publicated from 2006 to 2017. The percentage of cocoa in chocolate was minimal 10% and maximal 99%. Some location of company which produced in USA, France, Canada, U.K., Italy, Ecuador, etc. The range of rating is 1 to 5.
For of a better understanding we are going to change our variable name
#Q3
rename_dataset1 <-dataset1
rename_dataset1 <- rename_dataset1 %>% rename(Company_names = Company...Maker.if.known.,
Specific_Bean_Origin_or_Bar_Name= Specific.Bean.Origin.or.Bar.Name,
Cocoa_Percent = Cocoa.Percent,
Company_Location =Company.Location,
Bean_Type=Bean.Type,
Broad_Bean_Origin = Broad.Bean.Origin,
Review_Date =Review.Date,
Review_Entry_ID = REF)
head(rename_dataset1)
## Company_names Specific_Bean_Origin_or_Bar_Name Review_Entry_ID Review_Date
## 1 A. Morin Agua Grande 1876 2016
## 2 A. Morin Kpime 1676 2015
## 3 A. Morin Atsane 1676 2015
## 4 A. Morin Akata 1680 2015
## 5 A. Morin Quilla 1704 2015
## 6 A. Morin Carenero 1315 2014
## Cocoa_Percent Company_Location Rating Bean_Type Broad_Bean_Origin
## 1 63% France 3.75 Sao Tome
## 2 70% France 2.75 Togo
## 3 70% France 3.00 Togo
## 4 70% France 3.50 Togo
## 5 70% France 3.50 Peru
## 6 70% France 2.75 Criollo Venezuela
We are going to check for na value in the different colums
colSums(is.na(rename_dataset1))
## Company_names Specific_Bean_Origin_or_Bar_Name
## 0 0
## Review_Entry_ID Review_Date
## 0 0
## Cocoa_Percent Company_Location
## 0 0
## Rating Bean_Type
## 0 0
## Broad_Bean_Origin
## 0
Being given that we want to know which company produce the best chocolate we should start by identify every single company in the data set
#Q4
company_name <- unique(rename_dataset1$Company_names)
head(company_name)
## [1] "A. Morin" "Acalli" "Adi"
## [4] "Aequare (Gianduja)" "Ah Cacao" "Akesson's (Pralus)"
There are exactly 416 companies.
The use of the glimpse, the
summary and colsums functions allow me to have a better understanding of
my data set
Where are the best cocoa beans grown?
#Q6
best_cocoa_grown <- rename_dataset1 %>%
select(Broad_Bean_Origin, Rating) %>%
group_by(Broad_Bean_Origin) %>%
summarise(average_rating = mean(Rating))
head(best_cocoa_grown)
## # A tibble: 6 × 2
## Broad_Bean_Origin average_rating
## <chr> <dbl>
## 1 "" 2.5
## 2 "Africa, Carribean, C. Am." 2.75
## 3 "Australia" 3.25
## 4 "Belize" 3.23
## 5 "Bolivia" 3.20
## 6 "Brazil" 3.28
The above code grouped all the values of broad_bean_origin and calculated the average rating. The average_rating will be the metric to determine the cocoa beans grown.
cocoa_count <- c(
sum(best_cocoa_grown$average_rating < 6 & best_cocoa_grown$average_rating >= 5),
sum(best_cocoa_grown$average_rating < 5 & best_cocoa_grown$average_rating >= 4),
sum(best_cocoa_grown$average_rating < 4 & best_cocoa_grown$average_rating >= 3),
sum(best_cocoa_grown$average_rating < 3 & best_cocoa_grown$average_rating >= 2),
sum(best_cocoa_grown$average_rating < 2 & best_cocoa_grown$average_rating >= 1)
)
head(cocoa_count)
## [1] 0 6 74 21 0
df_cocoa_count <- data.frame(
Ratings = c("5", "4", "3", "2", "1"),
Counts = cocoa_count
)
library(ggplot2)
ggplot(df_cocoa_count, aes(Ratings, Counts)) + geom_bar(stat = "identity", fill = "skyblue") +
geom_text(aes(label = Counts), nudge_y = 2) +
labs(title = "Counts of each Ratings")
best_cocoa_grown %>%
filter(average_rating >= 4)
## # A tibble: 6 × 2
## Broad_Bean_Origin average_rating
## <chr> <dbl>
## 1 Dom. Rep., Madagascar 4
## 2 Gre., PNG, Haw., Haiti, Mad 4
## 3 Guat., D.R., Peru, Mad., PNG 4
## 4 Peru, Dom. Rep 4
## 5 Ven, Bolivia, D.R. 4
## 6 Venezuela, Java 4
from this study that i have done with the line of code above now we know that :Dom. Rep., Madagascar ,Gre., PNG, Haw., Haiti, Mad ,Guat., D.R., Peru, Mad., PNG , Peru, Dom. Rep, Ven, Bolivia, D.R. ,Venezuela, Java are the place where we can get the best cocoa bean
Which countries produce the highest-rated bars?
#Q7
highest_rated_bar <- rename_dataset1 %>%
select(Company_Location, Rating) %>%
group_by(Company_Location) %>%
summarise(highest_rated = max(Rating))
head(highest_rated_bar )
## # A tibble: 6 × 2
## Company_Location highest_rated
## <chr> <dbl>
## 1 Amsterdam 3.75
## 2 Argentina 3.75
## 3 Australia 4
## 4 Austria 3.75
## 5 Belgium 4
## 6 Bolivia 3.75
highest_count <- c(
sum(highest_rated_bar$highest_rated < 6 & highest_rated_bar$highest_rated >= 5),
sum(highest_rated_bar$highest_rated < 5 & highest_rated_bar$highest_rated >= 4),
sum(highest_rated_bar$highest_rated < 4 & highest_rated_bar$highest_rated >= 3),
sum(highest_rated_bar$highest_rated < 3 & highest_rated_bar$highest_rated >= 2),
sum(highest_rated_bar$highest_rated < 2 & highest_rated_bar$highest_rated >= 1)
)
df_highest_count <- data.frame(
Ratings = c("5", "4", "3", "2", "1"),
Counts = highest_count
)
library(ggplot2)
ggplot(df_highest_count, aes(Ratings, Counts)) + geom_bar(stat = "identity", fill = "wheat2") +
geom_text(aes(label = Counts), nudge_y = 2) +
labs(title = "Counts of each Ratings")
filter(highest_rated_bar, highest_rated >= 5)
## # A tibble: 1 × 2
## Company_Location highest_rated
## <chr> <dbl>
## 1 Italy 5
this code help us to find the country with the highest rated bar and in our case this country is Italy
What are the top 3 countries produce the highest-rated
bars?
#Q8
agg_country_rate <- aggregate(Rating ~ Company_Location, data = rename_dataset1, FUN = max)
head(agg_country_rate[order(-agg_country_rate$Rating),],3)
## Company_Location Rating
## 30 Italy 5
## 3 Australia 4
## 5 Belgium 4
From this study i know that Italy ,Australia ,Belgium are the the top 3 countries who produce the highest-rated bars
How much percent of Cocoa which get the best Rating ?
#Q9
agg_rate <- aggregate(Rating ~ Cocoa_Percent, data = rename_dataset1, FUN = max)
head(agg_rate[order(-agg_rate$Rating),],3)
## Cocoa_Percent Rating
## 21 70% 5
## 10 60% 4
## 14 63% 4
Based on rating, the best chocolate has 70% of cocoa in chocolate
Is Italy using the best cocoa beans grown?
#Q10
rename_dataset1 %>%
select(Company_Location, Rating,Broad_Bean_Origin ) %>%
filter( Company_Location== "Italy",Rating== 5)
## Company_Location Rating Broad_Bean_Origin
## 1 Italy 5 Venezuela
## 2 Italy 5
this code show us that Italy use bean from Venezuela and from the previous analysis we know that Venezuela is among the best place where beam of cocoa grow
Which Company produced the best rating Chocolate in 2017
?
#Q11
choco_2017 <- rename_dataset1[rename_dataset1$Review_Date == 2017, ]
best_choco_2017 <- aggregate(Rating ~ Company_names + Company_Location, data = choco_2017, FUN = mean)
best_choco_2017[order(-best_choco_2017$Rating),]
## Company_names Company_Location Rating
## 6 Dick Taylor U.S.A. 3.750000
## 1 Smooth Chocolator, The Australia 3.500000
## 3 Alexandre Netherlands 3.500000
## 7 French Broad U.S.A. 3.500000
## 9 Madre U.S.A. 3.500000
## 2 Soul Canada 3.375000
## 8 Letterpress U.S.A. 3.375000
## 10 Spencer U.S.A. 3.333333
## 4 Beau Cacao U.K. 3.125000
## 5 Dalloway U.S.A. 2.750000
## 11 Xocolla U.S.A. 2.625000
library(ggplot2)
ggplot(best_choco_2017, aes(y = reorder(Company_names,Rating),x = Rating, fill = Company_Location)) +
geom_col() +
geom_text(aes(label = round(Rating,2), hjust = 1.4)) +
labs(x = "Rating", y = "Company", title = "Best Chocolate Company in 2017") +
theme_minimal()
dick taylor is the company with the best rating in 2017
What is the best bean_type which will be used to get 80% cocoa_percent ?
#Q12
rename_dataset1$Company_names<- as.character(rename_dataset1$Company_names)
rename_dataset1$ Specific_Bean_Origin_or_Bar_Name<-as.factor(rename_dataset1$Specific_Bean_Origin_or_Bar_Name)
rename_dataset1$Review_Entry_ID<- as.character(rename_dataset1$Review_Entry_ID)
rename_dataset1$Cocoa_Percent<-as.numeric(substr(rename_dataset1$Cocoa_Percent,1,2))
rename_dataset1$Company_Location <- as.factor(rename_dataset1$Company_Location)
rename_dataset1$Bean_Type <-as.factor(rename_dataset1$Bean_Type)
rename_dataset1$Broad_Bean_Origin <- as.factor(rename_dataset1$Broad_Bean_Origin)
agg_bean_percent <- aggregate(Cocoa_Percent ~ Bean_Type, data = rename_dataset1, FUN = mean)
head(agg_bean_percent[order(-agg_bean_percent$Cocoa_Percent),],3)
## Bean_Type Cocoa_Percent
## 15 Criollo (Ocumare) 80.0
## 19 Criollo, Forastero 76.5
## 30 Forastero(Arriba, CCN) 75.0
The average of Criollo(Ocumare) bean_type will be used to produced chocolate bars with 80% of cocoa.
Which type of bean appear the most in our database
#Q13
bean_counts <- rename_dataset1 %>%
group_by(Bean_Type) %>%
summarise(count = n())
library(ggplot2)
ggplot(bean_counts, aes(x = Bean_Type, y = count, fill = Bean_Type)) +
geom_bar(stat = "identity") +
labs(x = "Bean type", y = "Count", title = "Count of bean type") +
theme(legend.position = "none",
axis.text.x = element_text(angle = 90, hjust = 1))
what is the most highest rate of chocolate percent in the cocoa
percent colum
#Q14
dd2 <- rename_dataset1 %>%
group_by(Cocoa_Percent) %>%
summarise(Max_Rating = max(Rating), count1 = n())
head(dd2)
## # A tibble: 6 × 3
## Cocoa_Percent Max_Rating count1
## <dbl> <dbl> <int>
## 1 10 3.5 20
## 2 42 2.75 1
## 3 46 2.75 1
## 4 50 3.75 1
## 5 53 2 1
## 6 55 3.5 16
the highest rate of chocolate percent is 5 this mark is for a chocolate bar with 70 %
What’s the relationship between cocoa solids percentage and rating?In this question, we will use the columns cocoa_percent and rating to determine the correlation between cocoa solids percentage and rating.
#Q15
corr_cocoa_rating <- rename_dataset1 %>%
select(Cocoa_Percent, Rating)
library(ggplot2)
corr_r <- cor(corr_cocoa_rating$Cocoa_Percent, corr_cocoa_rating$Rating)
ggplot(corr_cocoa_rating, aes(x = Cocoa_Percent, y = Rating)) +
geom_point() +
geom_smooth(formula = y ~ x, method = lm, se = FALSE) +
annotate("text", x = 0.5, y = 4.5, label = paste("R = ", format(round(corr_r, 2), nsmall = 2))) +
labs(title = "Correlation of Cocoa Percentage and Ratings", x = "Cocoa percent")
There are little or no correlation between cocoa percent and rating