Noori Selina Discussion post #1:
Section WILA Exercise C10: In Example TMP the first table lists the cost (per kilogram) to manufacture each of the three varieties of trail mix (bulk, standard, fancy). For example, it costs $3.69 to make one kilogram of the bulk variety. Re-compute each of these three costs and notice that the computations are linear in character.
# The mix values
mix_bulk <- list(raisins = 7, peanuts = 6, chocolate = 2)
mix_standard <- list(raisins = 6, peanuts = 4, chocolate = 5)
mix_fancy <- list(raisins = 2, peanuts = 5, chocolate = 8)
# Ingredient costs
ingredient_costs <- c(raisins = 2.55, peanuts = 4.65, chocolate = 4.80)
# Calculating cost per kilogram for a mix
calculate_cost_per_kg <- function(ingredients_mix, costs) {
total_cost <- sum(mapply(function(x, y) x * y, ingredients_mix, costs))
cost_per_kg <- total_cost / 15 # Divide by 15kg to get cost per kilogram
return(cost_per_kg)
}
# Calculating cost per kilogram for each variety
cost_per_kg_bulk <- calculate_cost_per_kg(mix_bulk, ingredient_costs)
cost_per_kg_standard <- calculate_cost_per_kg(mix_standard, ingredient_costs)
cost_per_kg_fancy <- calculate_cost_per_kg(mix_fancy, ingredient_costs)
# Printing the costs
cost_per_kg_bulk
## [1] 3.69
cost_per_kg_standard
## [1] 3.86
cost_per_kg_fancy
## [1] 4.45