This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(readr)
data <- read_csv('avocado.csv')
## Rows: 12628 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): date, type, geography
## dbl (4): average_price, total_volume, year, Mileage
##
## ℹ 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.
summary(data)
## date average_price total_volume type
## Length :12628 Min. :0.500 Min. : 253 Length :12628
## N.unique : 154 1st Qu.:1.100 1st Qu.: 15733 N.unique : 2
## N.blank : 0 Median :1.320 Median : 94806 N.blank : 0
## Min.nchar: 8 Mean :1.359 Mean : 325259 Min.nchar: 7
## Max.nchar: 10 3rd Qu.:1.570 3rd Qu.: 430222 Max.nchar: 12
## Max. :2.780 Max. :5660216
## year geography Mileage
## Min. :2017 Length :12628 Min. : 111
## 1st Qu.:2018 N.unique : 41 1st Qu.:1097
## Median :2019 N.blank : 0 Median :2193
## Mean :2019 Min.nchar: 5 Mean :1911
## 3rd Qu.:2020 Max.nchar: 20 3rd Qu.:2632
## Max. :2020 Max. :2998
## DataViz
hist(data$average_price,
main = "Histogram of average_price",
xlab = "Price in USD (US Dollar)")
library(ggplot2)
ggplot(data, aes(x = average_price, fill = type)) +
geom_histogram(bins = 30, col = "black") +
scale_fill_manual(values = c("yellow", "blue")) +
ggtitle("Frequency of Average Price - Organic vs. Conventional")
ggplot() +
geom_col(data, mapping = aes(x = reorder(geography, total_volume),
y = total_volume, fill = year)) +
xlab("geography") +
ylab("total_volume") +
theme(axis.text.x = element_text(angle = 90, size = 7))
summary(data$average_price)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.500 1.100 1.320 1.359 1.570 2.780
data[which.min(data$average_price), ]
## # A tibble: 1 × 7
## date average_price total_volume type year geography Mileage
## <chr> <dbl> <dbl> <chr> <dbl> <chr> <dbl>
## 1 2018/4/22 0.5 2335867 conventional 2018 Houston 1656
data[which.max(data$average_price), ]
## # A tibble: 1 × 7
## date average_price total_volume type year geography Mileage
## <chr> <dbl> <dbl> <chr> <dbl> <chr> <dbl>
## 1 2019/7/14 2.78 15994 organic 2019 San Diego 253
Prices ranged from $0.50 (conventional, Houston, week of April 22, 2018) to $2.78 (organic, San Diego, week of July 14, 2019). Stakeholders could use HAB’s research to plan supply (producers), set pricing and promotions (retailers), and run marketing campaigns (marketers/health professionals).
data$dollar_sales <- data$average_price * data$total_volume
sales <- aggregate(dollar_sales ~ year + type + geography,
data = subset(data, year %in% c(2017, 2018)),
FUN = sum)
top_city <- sales[order(sales$year, sales$type, -sales$dollar_sales), ]
top_city <- do.call(rbind, by(top_city, list(top_city$year, top_city$type), head, n = 1))
top_city$dollar_sales <- format(round(top_city$dollar_sales), big.mark = ",")
top_city[order(top_city$year, top_city$type), ]
## year type geography dollar_sales
## 81 2017 conventional Los Angeles 13,456,647
## 103 2017 organic New York 720,451
## 82 2018 conventional Los Angeles 153,526,216
## 104 2018 organic New York 9,391,204
Los Angeles led conventional sales and New York led organic sales in both 2017 and 2018. This is likely due to larger population, higher grocery spending, and a strong local demand for organic/healthy foods.
weekly <- aggregate(average_price ~ date + type, data = data, FUN = mean)
ggplot(weekly, aes(x = date, y = average_price, color = type)) +
geom_line() +
labs(title = "Weekly Average Avocado Price Over Time",
x = NULL, y = "Average Price (USD)", color = "Type")
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
city <- aggregate(cbind(average_price, Mileage) ~ geography + type, data = data, FUN = mean)
ggplot(city, aes(x = Mileage, y = average_price)) +
geom_point(color = "darkgreen") +
geom_smooth(method = "lm", se = FALSE, color = "black") +
facet_wrap(~ type) +
labs(title = "Distance from Growing Region vs. Average Price",
x = "Mileage", y = "Average Price (USD)")
## `geom_smooth()` using formula = 'y ~ x'
Figure 1 shows organic avocados cost more than conventional every week, with prices rising in summer and dropping in winter. Figure 2 checks whether distance from the growing region raises prices — a rising trend line would suggest shipping distance adds cost.