In my project I took data set called Recipes and Reviews from Kaggle where there are over 522,517 recipes. I analyzed all this data that included data frames: name, ratings, reviews, ingredients, recipeIDs and descriptions. With this data I wanted to see what were some of the top ranked recipes, the most common ingredients, the most common recipes, and the sentiments behind the highest ranked recipes and least ranked recipes as well as which category had the most positive sentiments and lastly testing whether my least favorite foods were in recipes with positive sentiment attached to them. This code is very unique and complex especially with so many recipes so keep that in mind when observing my work.
Load the following packages:
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.4.4 ✔ 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
library(readr)library(dplyr)library(tidytext)
1. Merging the data
The first step was to merge the recipes and the reviews by the RecipeId by creating a new variable called merged. This merged function now has both recipes and reviews and I can use it to make updated data sets.
recipes <-read_csv("recipes.csv")
Rows: 522517 Columns: 28
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (13): Name, AuthorName, CookTime, PrepTime, TotalTime, Description, Ima...
dbl (14): RecipeId, AuthorId, AggregatedRating, ReviewCount, Calories, FatC...
dttm (1): DatePublished
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
reviews <-read_csv("reviews.csv")
Rows: 1401982 Columns: 8
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): AuthorName, Review
dbl (4): ReviewId, RecipeId, AuthorId, Rating
dttm (2): DateSubmitted, DateModified
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
The next step was to filter the data set by creating a new one that only included the data I needed. new_data included RecipeId, Name, RecipeIngredientParts, Rating, Description, Review, and RecipeCategory. Then I took this data and took roughly 30 recipes to see what was ranked a 5. This included beverages, desserts and some meals. To see the diversity of highly ranked recipes. I also used the the distinct function to limit an repeated recipes.
Next I wanted to visualize some of the ingredients found in the recipes and see if any of them stuck out. I did this by making a new data set called new_data2 that unnested the ingredient parts to be individual values.I used only 5 rated and specifically non beverage recipes. And there were many words that showed up but the main ones to me were sugar, salt, milk, eggs, and chocolate. So I concluded that dessert ingredients are very prominent so then I wanted to look solely at that category.
When I took apart the categories and plotted them. It shows that dessert is the most common recipe as well as rated category. So specifically what about these recipes had reviews that made them ranked 5. So I took apart the sentiments of those ranked a 5 and 1 to see what their reviews sentiments looked like.
when pulling this first bit of data I just observed some of the positive sentiment words such as fresh, sweet and warm. But I wanted more significant data so in this next step I am pulling the sentiment from ratings of 5 and seeing more closer their sentiment and compare to those with a 1 rating.
Joining with `by = join_by(word)`
Joining with `by = join_by(word)`
# A tibble: 65 × 3
word n value
<chr> <int> <dbl>
1 fresh 281215 1
2 crushed 28002 -2
3 sweet 26129 2
4 free 12336 1
5 warm 11885 1
6 active 10950 1
7 prepared 8991 1
8 cool 8404 1
9 hard 5957 -1
10 nuts 5251 -3
# ℹ 55 more rows
6. Positive Sentiment for Best Ranked Recipes
My next step was seeing which category had the most positive sentiment so I am pulling the sentiment from ratings of 5 and finding the 12 most popular recipes.I am pulling the sentiment from ratings of 5 and finding the 12 most popular recipes pulled all categories and tested to see which one had the most positive sentiment.
I found that the most popular recipes were Layered Salad, Tomato Sandwich, BLT and Ham Salad. Then took apart the top 12 recipes and looked at the top ingredients in those recipes that contained the most sentiment which were tomato, bread, and ham. This is data is repeated and a little messy so that is why words such as sandwich and summer.
7. Lastly, Found Category with the most Positive Sentiment.
My next step was seeing which category had the most positive sentiment so I pulled all categories and tested to see which one had the most positive sentiment. The data shows that Category bean soup, meatloaf, and dehydrator these categories had the highest positive sentiment.
category_sentiments %>%head(20) %>%ggplot(aes(reorder(RecipeCategory, avg_sentiment), avg_sentiment, fill = RecipeCategory)) +geom_col() +coord_flip()
Conclusion
In conclusion, when looking at the 522,517 recipes, I found that some of the most common recipes in the highest ranking were desserts and this is not only proven by the ggplot but also by the wordcloud that consisted of the most common ingredients being sugar, eggs and milk. Then I looked at the sentiment of all the recipes and their reviews (keep in mind there are duplicates and this data is very messy). But I found that sandwich recipes and salads in the 5th ranking contained the most positive sentiments specifically for its ingredients of bread, and tomato. Lastly, I found that Bean Soup recipes, and Meatloaf recipes were two of the highest ranked categorizes according to their sentiment. So if you were to use this data set for recipes I suggest you use their Soup, Salad, meatloaf and and inexpensive meals cook from and they will taste the most delectable.