Introduction

This vignette is focused on the purrr and forcats packages from the Tidyverse, and were chosen based on the simple logic that I am the least unfamiliar with them so it seemed like a good opportunity to change that.

Load packages

Loading the two packages directly, though you could also load the whole tidyverse package

library(purrr)
library(forcats)
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

Load the data

I used a dataset from Kaggle with information on Indian cuisine (link and info below. It is stored in Github for reproducibility.

https://www.kaggle.com/nehaprabhavalkar/indian-food-101 Version 2, Accessed 26 October 2020

file <- "https://raw.githubusercontent.com/cwestsmith/cuny-msds/master/datasets/indian_food.csv"
df <- read.csv(file, header = TRUE)
# Checking structure of new data frame.  Lots of factors, which is good for this vignette
str(df)
## 'data.frame':    255 obs. of  9 variables:
##  $ name          : chr  "Balu shahi" "Boondi" "Gajar ka halwa" "Ghevar" ...
##  $ ingredients   : chr  "Maida flour, yogurt, oil, sugar" "Gram flour, ghee, sugar" "Carrots, milk, sugar, ghee, cashews, raisins" "Flour, ghee, kewra, milk, clarified butter, sugar, almonds, pistachio, saffron, green cardamom" ...
##  $ diet          : chr  "vegetarian" "vegetarian" "vegetarian" "vegetarian" ...
##  $ prep_time     : int  45 80 15 15 15 10 10 10 20 10 ...
##  $ cook_time     : int  25 30 60 30 40 50 50 20 30 40 ...
##  $ flavor_profile: chr  "sweet" "sweet" "sweet" "sweet" ...
##  $ course        : chr  "dessert" "dessert" "dessert" "dessert" ...
##  $ state         : chr  "West Bengal" "Rajasthan" "Punjab" "Rajasthan" ...
##  $ region        : chr  "East" "West" "North" "West" ...

Example 1: Forcats

Forcats is focused on making it easier to work with factors in R. One such function is fct_infreq(), which reorders a factor based on the frequency of values it has. In the example below I will use this function to reorder the flavor_profile variable based on the frequency of its levels in the dataset.

# View current structure of flavor_profile factor
levels(df$flavor_profile)
## NULL
# View summary of each flavor_profile based on # of entries
df %>% group_by(flavor_profile) %>% summarise(n())
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 5 x 2
##   flavor_profile `n()`
##   <chr>          <int>
## 1 -1                29
## 2 bitter             4
## 3 sour               1
## 4 spicy            133
## 5 sweet             88
# Re-order the flavor_profile
df$flavor_profile <- fct_infreq(df$flavor_profile)
# Now view it again, which should match the order per the summary above
levels(df$flavor_profile)
## [1] "spicy"  "sweet"  "-1"     "bitter" "sour"

Example 2: purrr

Purr is focused on making it easier to work with functions and vectors in R. One such function is pluck() which provides a flexible way to quickly extract an element. It has a sister function named chuck() which is similar except that it throws errors when something does not exist, rather than simply returning NULL.

# Create an example list
example_list <- as.list(df$ingredients)
# Pluck an element by index
pluck(example_list, 1)
## [1] "Maida flour, yogurt, oil, sugar"
# Searching for element that does not exist - pluck returns NULL
pluck(example_list, 100000)
## NULL
# But chuck returns an error
#chuck(example_list, 100000)

Conclusion

In conclusion Forcats and Purr both include a wide array of useful functions which make it easier to work with various types of data in R.

From here onwards Extended by – Peter Fernandes

Capability 1 stringr::str_subset

Creating subset from the main set

# str_subset(string, pattern, negate = FALSE)

#flavor profile starting with - s
stringr::str_subset(df$flavor_profile, "^s")
##   [1] "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet"
##  [10] "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet"
##  [19] "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet"
##  [28] "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet"
##  [37] "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet"
##  [46] "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet"
##  [55] "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet" "sweet"
##  [64] "sweet" "spicy" "spicy" "sweet" "spicy" "spicy" "spicy" "spicy" "spicy"
##  [73] "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy"
##  [82] "spicy" "spicy" "sweet" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy"
##  [91] "spicy" "spicy" "spicy" "sweet" "spicy" "spicy" "spicy" "spicy" "spicy"
## [100] "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "sweet" "spicy"
## [109] "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "sweet" "spicy"
## [118] "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy"
## [127] "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy"
## [136] "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "sweet" "spicy" "spicy"
## [145] "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy"
## [154] "spicy" "spicy" "sweet" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy"
## [163] "spicy" "sweet" "spicy" "sweet" "spicy" "spicy" "sweet" "spicy" "sour" 
## [172] "spicy" "spicy" "spicy" "spicy" "spicy" "sweet" "spicy" "spicy" "spicy"
## [181] "spicy" "spicy" "spicy" "spicy" "sweet" "spicy" "spicy" "spicy" "spicy"
## [190] "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "sweet" "sweet"
## [199] "sweet" "spicy" "spicy" "spicy" "spicy" "spicy" "sweet" "sweet" "spicy"
## [208] "spicy" "spicy" "spicy" "spicy" "spicy" "spicy" "sweet" "sweet" "spicy"
## [217] "spicy" "sweet" "sweet" "sweet" "sweet" "sweet"

Capability 2 stringr::str_sort

Sort character vector

#str_sort(x, decreasing = FALSE, na_last = TRUE, locale = “en”,numeric = FALSE, …)

#decreasing= FALSE for Ascending order sort
stringr::str_sort(df$name, decreasing = FALSE)
##   [1] "Adhirasam"                    "Aloo gobi"                   
##   [3] "Aloo matar"                   "Aloo methi"                  
##   [5] "Aloo shimla mirch"            "Aloo tikki"                  
##   [7] "Alu Pitika"                   "Amti"                        
##   [9] "Anarsa"                       "Ariselu"                     
##  [11] "Attu"                         "Avial"                       
##  [13] "Bajri no rotlo"               "Balu shahi"                  
##  [15] "Bandar laddu"                 "Basundi"                     
##  [17] "Bebinca"                      "Beef Fry"                    
##  [19] "Bengena Pitika"               "Bhakri"                      
##  [21] "Bhatura"                      "Bhindi masala"               
##  [23] "Bilahi Maas"                  "Biryani"                     
##  [25] "Bisi bele bath"               "Black rice"                  
##  [27] "Bombil fry"                   "Boondi"                      
##  [29] "Bora Sawul"                   "Brown Rice"                  
##  [31] "Butter chicken"               "Chak Hao Kheer"              
##  [33] "Chakali"                      "Cham cham"                   
##  [35] "Chana masala"                 "Chapati"                     
##  [37] "Cheera Doi"                   "Chevdo"                      
##  [39] "Chhena jalebi"                "Chhena kheeri"               
##  [41] "Chhena poda"                  "Chicken razala"              
##  [43] "Chicken Tikka"                "Chicken Tikka masala"        
##  [45] "Chicken Varuval"              "Chikki"                      
##  [47] "Chingri Bhape"                "Chingri malai curry"         
##  [49] "Chole bhature"                "Chorafali"                   
##  [51] "Churma Ladoo"                 "Coconut vadi"                
##  [53] "Copra paak"                   "Currivepillai sadam "        
##  [55] "Daal baati churma"            "Daal Dhokli"                 
##  [57] "Daal puri"                    "Dahi vada"                   
##  [59] "Dal makhani "                 "Dal tadka"                   
##  [61] "Dalithoy"                     "Dharwad pedha"               
##  [63] "Dhokla"                       "Dhondas"                     
##  [65] "Doodhpak"                     "Dosa"                        
##  [67] "Double ka meetha"             "Dudhi halwa"                 
##  [69] "Dum aloo"                     "Fara"                        
##  [71] "Farsi Puri"                   "Gajar ka halwa"              
##  [73] "Galho"                        "Gatta curry"                 
##  [75] "Gavvalu"                      "Gheela Pitha"                
##  [77] "Ghevar"                       "Ghooghra"                    
##  [79] "Goja"                         "Gud papdi"                   
##  [81] "Gulab jamun"                  "Halvasan"                    
##  [83] "Hando Guri"                   "Handwo"                      
##  [85] "Haq Maas"                     "Idiappam"                    
##  [87] "Idli"                         "Imarti"                      
##  [89] "Jalebi"                       "Jeera Aloo"                  
##  [91] "Kaara kozhambu"               "Kabiraji"                    
##  [93] "Kachori"                      "Kadai paneer"                
##  [95] "Kadhi pakoda"                 "Kajjikaya"                   
##  [97] "Kaju katli"                   "Kakinada khaja"              
##  [99] "Kalakand"                     "Kanji"                       
## [101] "Kansar"                       "Karela bharta"               
## [103] "Keerai kootu"                 "Keerai masiyal"              
## [105] "Keerai poriyal"               "Keerai sadam"                
## [107] "Keri no ras"                  "Khakhra"                     
## [109] "Khaman"                       "Khandvi"                     
## [111] "Khar"                         "Kheer"                       
## [113] "Kheer sagar"                  "Khichdi"                     
## [115] "Khichu"                       "Khorisa"                     
## [117] "Kofta"                        "Koldil Chicken"              
## [119] "Koldil Duck"                  "Kolim Jawla"                 
## [121] "Kombdi vade"                  "Konir Dom"                   
## [123] "Kootu"                        "Kos kootu"                   
## [125] "Koshambri"                    "Koshimbir"                   
## [127] "Kothamali sadam"              "Kulfi falooda"               
## [129] "Kumol Sawul"                  "Kutchi dabeli"               
## [131] "Kuzhakkattai"                 "Kuzhambu"                    
## [133] "Kuzhi paniyaram"              "Laapsi"                      
## [135] "Laddu"                        "Lassi"                       
## [137] "Lauki ke kofte"               "Lauki ki subji"              
## [139] "Ledikeni"                     "Lilva Kachori"               
## [141] "Litti chokha"                 "Luchi"                       
## [143] "Lyangcha"                     "Maach Jhol"                  
## [145] "Mag Dhokli"                   "Mahim halwa"                 
## [147] "Makki di roti sarson da saag" "Malapua"                     
## [149] "Masala Dosa"                  "Masor Koni"                  
## [151] "Masor tenga"                  "Mawa Bati"                   
## [153] "Methi na Gota"                "Mihidana"                    
## [155] "Mishti Chholar Dal"           "Misi roti"                   
## [157] "Misti doi"                    "Modak"                       
## [159] "Mohanthal"                    "Mushroom do pyaza"           
## [161] "Mushroom matar"               "Muthiya"                     
## [163] "Mysore pak"                   "Naan"                        
## [165] "Namakpara"                    "Nankhatai"                   
## [167] "Navrattan korma"              "Obbattu holige"              
## [169] "Pachadi"                      "Pakhala"                     
## [171] "Palak paneer"                 "Palathalikalu"               
## [173] "Paneer butter masala"         "Paneer tikka masala"         
## [175] "Pani Pitha"                   "Pani puri"                   
## [177] "Paniyaram"                    "Panjeeri"                    
## [179] "Pantua"                       "Papad"                       
## [181] "Papadum"                      "Paratha"                     
## [183] "Paravannam"                   "Paruppu sadam"               
## [185] "Patra"                        "Pattor"                      
## [187] "Pav Bhaji"                    "Payasam"                     
## [189] "Payokh"                       "Pesarattu"                   
## [191] "Petha"                        "Phirni"                      
## [193] "Pinaca"                       "Pindi chana"                 
## [195] "Pithe"                        "Poha"                        
## [197] "Pongal"                       "Poornalu"                    
## [199] "Pootharekulu"                 "Poriyal"                     
## [201] "Pork Bharta"                  "Prawn malai curry"           
## [203] "Puli sadam"                   "Puri Bhaji"                  
## [205] "Puttu"                        "Qubani ka meetha"            
## [207] "Rabri"                        "Rajma chaval"                
## [209] "Ras malai"                    "Rasabali"                    
## [211] "Rasam"                        "Rasgulla"                    
## [213] "Red Rice"                     "Rongi"                       
## [215] "Saath"                        "Sabudana Khichadi"           
## [217] "Sambar"                       "Samosa"                      
## [219] "Sandesh"                      "Sandige"                     
## [221] "Sattu ki roti"                "Sev khamani"                 
## [223] "Sev tameta"                   "Sevai"                       
## [225] "Shahi paneer"                 "Shahi tukra"                 
## [227] "Shankarpali"                  "Sheer korma"                 
## [229] "Sheera"                       "Shrikhand"                   
## [231] "Shufta"                       "Shukto"                      
## [233] "Singori"                      "Sohan halwa"                 
## [235] "Sohan papdi"                  "Sukhdi"                      
## [237] "Surnoli"                      "Sutar feni"                  
## [239] "Tandoori Chicken"             "Tandoori Fish Tikka"         
## [241] "Thalipeeth"                   "Thayir sadam"                
## [243] "Theeyal"                      "Thepla"                      
## [245] "Til Pitha"                    "Turiya Patra Vatana sabji"   
## [247] "Undhiyu"                      "Unni Appam"                  
## [249] "Upma"                         "Uttapam"                     
## [251] "Vada"                         "Veg Kolhapuri"               
## [253] "Vegetable jalfrezi"           "Vindaloo"                    
## [255] "Zunka"
#decreasing= TRUE for Descending order sort
stringr::str_sort(df$name, decreasing = TRUE)
##   [1] "Zunka"                        "Vindaloo"                    
##   [3] "Vegetable jalfrezi"           "Veg Kolhapuri"               
##   [5] "Vada"                         "Uttapam"                     
##   [7] "Upma"                         "Unni Appam"                  
##   [9] "Undhiyu"                      "Turiya Patra Vatana sabji"   
##  [11] "Til Pitha"                    "Thepla"                      
##  [13] "Theeyal"                      "Thayir sadam"                
##  [15] "Thalipeeth"                   "Tandoori Fish Tikka"         
##  [17] "Tandoori Chicken"             "Sutar feni"                  
##  [19] "Surnoli"                      "Sukhdi"                      
##  [21] "Sohan papdi"                  "Sohan halwa"                 
##  [23] "Singori"                      "Shukto"                      
##  [25] "Shufta"                       "Shrikhand"                   
##  [27] "Sheera"                       "Sheer korma"                 
##  [29] "Shankarpali"                  "Shahi tukra"                 
##  [31] "Shahi paneer"                 "Sevai"                       
##  [33] "Sev tameta"                   "Sev khamani"                 
##  [35] "Sattu ki roti"                "Sandige"                     
##  [37] "Sandesh"                      "Samosa"                      
##  [39] "Sambar"                       "Sabudana Khichadi"           
##  [41] "Saath"                        "Rongi"                       
##  [43] "Red Rice"                     "Rasgulla"                    
##  [45] "Rasam"                        "Rasabali"                    
##  [47] "Ras malai"                    "Rajma chaval"                
##  [49] "Rabri"                        "Qubani ka meetha"            
##  [51] "Puttu"                        "Puri Bhaji"                  
##  [53] "Puli sadam"                   "Prawn malai curry"           
##  [55] "Pork Bharta"                  "Poriyal"                     
##  [57] "Pootharekulu"                 "Poornalu"                    
##  [59] "Pongal"                       "Poha"                        
##  [61] "Pithe"                        "Pindi chana"                 
##  [63] "Pinaca"                       "Phirni"                      
##  [65] "Petha"                        "Pesarattu"                   
##  [67] "Payokh"                       "Payasam"                     
##  [69] "Pav Bhaji"                    "Pattor"                      
##  [71] "Patra"                        "Paruppu sadam"               
##  [73] "Paravannam"                   "Paratha"                     
##  [75] "Papadum"                      "Papad"                       
##  [77] "Pantua"                       "Panjeeri"                    
##  [79] "Paniyaram"                    "Pani puri"                   
##  [81] "Pani Pitha"                   "Paneer tikka masala"         
##  [83] "Paneer butter masala"         "Palathalikalu"               
##  [85] "Palak paneer"                 "Pakhala"                     
##  [87] "Pachadi"                      "Obbattu holige"              
##  [89] "Navrattan korma"              "Nankhatai"                   
##  [91] "Namakpara"                    "Naan"                        
##  [93] "Mysore pak"                   "Muthiya"                     
##  [95] "Mushroom matar"               "Mushroom do pyaza"           
##  [97] "Mohanthal"                    "Modak"                       
##  [99] "Misti doi"                    "Misi roti"                   
## [101] "Mishti Chholar Dal"           "Mihidana"                    
## [103] "Methi na Gota"                "Mawa Bati"                   
## [105] "Masor tenga"                  "Masor Koni"                  
## [107] "Masala Dosa"                  "Malapua"                     
## [109] "Makki di roti sarson da saag" "Mahim halwa"                 
## [111] "Mag Dhokli"                   "Maach Jhol"                  
## [113] "Lyangcha"                     "Luchi"                       
## [115] "Litti chokha"                 "Lilva Kachori"               
## [117] "Ledikeni"                     "Lauki ki subji"              
## [119] "Lauki ke kofte"               "Lassi"                       
## [121] "Laddu"                        "Laapsi"                      
## [123] "Kuzhi paniyaram"              "Kuzhambu"                    
## [125] "Kuzhakkattai"                 "Kutchi dabeli"               
## [127] "Kumol Sawul"                  "Kulfi falooda"               
## [129] "Kothamali sadam"              "Koshimbir"                   
## [131] "Koshambri"                    "Kos kootu"                   
## [133] "Kootu"                        "Konir Dom"                   
## [135] "Kombdi vade"                  "Kolim Jawla"                 
## [137] "Koldil Duck"                  "Koldil Chicken"              
## [139] "Kofta"                        "Khorisa"                     
## [141] "Khichu"                       "Khichdi"                     
## [143] "Kheer sagar"                  "Kheer"                       
## [145] "Khar"                         "Khandvi"                     
## [147] "Khaman"                       "Khakhra"                     
## [149] "Keri no ras"                  "Keerai sadam"                
## [151] "Keerai poriyal"               "Keerai masiyal"              
## [153] "Keerai kootu"                 "Karela bharta"               
## [155] "Kansar"                       "Kanji"                       
## [157] "Kalakand"                     "Kakinada khaja"              
## [159] "Kaju katli"                   "Kajjikaya"                   
## [161] "Kadhi pakoda"                 "Kadai paneer"                
## [163] "Kachori"                      "Kabiraji"                    
## [165] "Kaara kozhambu"               "Jeera Aloo"                  
## [167] "Jalebi"                       "Imarti"                      
## [169] "Idli"                         "Idiappam"                    
## [171] "Haq Maas"                     "Handwo"                      
## [173] "Hando Guri"                   "Halvasan"                    
## [175] "Gulab jamun"                  "Gud papdi"                   
## [177] "Goja"                         "Ghooghra"                    
## [179] "Ghevar"                       "Gheela Pitha"                
## [181] "Gavvalu"                      "Gatta curry"                 
## [183] "Galho"                        "Gajar ka halwa"              
## [185] "Farsi Puri"                   "Fara"                        
## [187] "Dum aloo"                     "Dudhi halwa"                 
## [189] "Double ka meetha"             "Dosa"                        
## [191] "Doodhpak"                     "Dhondas"                     
## [193] "Dhokla"                       "Dharwad pedha"               
## [195] "Dalithoy"                     "Dal tadka"                   
## [197] "Dal makhani "                 "Dahi vada"                   
## [199] "Daal puri"                    "Daal Dhokli"                 
## [201] "Daal baati churma"            "Currivepillai sadam "        
## [203] "Copra paak"                   "Coconut vadi"                
## [205] "Churma Ladoo"                 "Chorafali"                   
## [207] "Chole bhature"                "Chingri malai curry"         
## [209] "Chingri Bhape"                "Chikki"                      
## [211] "Chicken Varuval"              "Chicken Tikka masala"        
## [213] "Chicken Tikka"                "Chicken razala"              
## [215] "Chhena poda"                  "Chhena kheeri"               
## [217] "Chhena jalebi"                "Chevdo"                      
## [219] "Cheera Doi"                   "Chapati"                     
## [221] "Chana masala"                 "Cham cham"                   
## [223] "Chakali"                      "Chak Hao Kheer"              
## [225] "Butter chicken"               "Brown Rice"                  
## [227] "Bora Sawul"                   "Boondi"                      
## [229] "Bombil fry"                   "Black rice"                  
## [231] "Bisi bele bath"               "Biryani"                     
## [233] "Bilahi Maas"                  "Bhindi masala"               
## [235] "Bhatura"                      "Bhakri"                      
## [237] "Bengena Pitika"               "Beef Fry"                    
## [239] "Bebinca"                      "Basundi"                     
## [241] "Bandar laddu"                 "Balu shahi"                  
## [243] "Bajri no rotlo"               "Avial"                       
## [245] "Attu"                         "Ariselu"                     
## [247] "Anarsa"                       "Amti"                        
## [249] "Alu Pitika"                   "Aloo tikki"                  
## [251] "Aloo shimla mirch"            "Aloo methi"                  
## [253] "Aloo matar"                   "Aloo gobi"                   
## [255] "Adhirasam"

Capability 3 ggplot2::ggplot

ggplot provides extensive data visualization options which helps to understand the underlying data easily and quickly

library(ggplot2)
df1 <- df %>% group_by(flavor_profile)
ggplot(data = df1, aes(x=df1$flavor_profile,  y=df$prep_time)) + geom_bar(stat = "identity", fill = "steelblue") + xlab("Flavors") + ylab("Preparation time") + ggtitle("Flavors with Preparation time") + theme(plot.title = element_text(hjust = 0.5))