Code
library(dplyr)
library(here)
library(readxl)
library(readr)
library(tidyverse)
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)library(dplyr)
library(here)
library(readxl)
library(readr)
library(tidyverse)
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)Today’s challenge is to:
pivot_longer() or pivot_wider().Read in one (or more) of the following datasets, using the correct R package and command.
# Read in organic egg poultry data set
eggs_poultry <- read_excel(here("data", "organiceggpoultry.xls"))
head(eggs_poultry)# A tibble: 6 × 11
(Certified Organic den…¹ ...2 ...3 ...4 ...5 ...6 ...7 ...8 ...9 ...10
<chr> <chr> <chr> <chr> <chr> <lgl> <chr> <chr> <chr> <chr>
1 <NA> <NA> <NA> <NA> <NA> NA <NA> <NA> <NA> <NA>
2 USDA Certified Organic … <NA> <NA> <NA> <NA> NA USDA… <NA> <NA> <NA>
3 Price per Carton Delive… <NA> <NA> <NA> <NA> NA Pric… <NA> <NA> <NA>
4 <NA> "Ext… "Ext… "Lar… "Lar… NA Whole B/S … Bone… Whol…
5 Jan 2004 "230" "132" "230" "126" NA 197.5 645.5 too … 193.5
6 February "230" "134… "226… "128… NA 197.5 642.5 too … 193.5
# ℹ abbreviated name:
# ¹`(Certified Organic denotes products grown and processed according to USDA's national organic standards and certified by USDA-accredited State and private certification organizations.)`
# ℹ 1 more variable: ...11 <chr>
Describe the data, and be sure to comment on why you are planning to pivot it to make it “tidy”.
Looking at the initial data shows what we must first do to tidy the data to make it usable. First we need to skip the first 5 rows for erroneous headers and annotations. We should also rename the columns to something more friendly to work with. There is an empty column we must remove separating possibly 2 distinct data sets, being egg and poultry. We must also clean up the “date” column that sometimes has “month, year”, sometimes just month, and sometimes with a trailing annotation. There are also 2 columns that have a couple string values that need to be treated numerically and can be dropped as NA values.
# Read in data, skipping first rows, giving custom names to columns, and removing empty column.
eggs_poultry <- read_excel(here("data", "organiceggpoultry.xls"),
skip = 5,
col_names = c("date", "xlarge_dozen", "xlarge_half_dozen",
"large_dozen", "large_half_dozen", "delete",
"whole", "bs_breast", "bone_breast", "whole_legs",
"thighs")) %>%
select(!contains("delete"))
# Remove /1 note suffix from date column
eggs_poultry$date <- sub(" /1", "", eggs_poultry$date)
# Separate month and year into their own columns. Fill the remaining year values
eggs_poultry <- eggs_poultry %>%
separate(date, " ", into=c("month", "year")) %>%
fill(year)
# Mutate the 2 columns that have text for values to be numeric
eggs_poultry <- eggs_poultry %>%
mutate_at(c('bone_breast', 'thighs'), as.numeric)
head(eggs_poultry)# A tibble: 6 × 11
month year xlarge_dozen xlarge_half_dozen large_dozen large_half_dozen whole
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Jan 2004 230 132 230 126 198.
2 Febru… 2004 230 134. 226. 128. 198.
3 March 2004 230 137 225 131 209
4 April 2004 234. 137 225 131 212
5 May 2004 236 137 225 131 214.
6 June 2004 241 137 231. 134. 216.
# ℹ 4 more variables: bs_breast <dbl>, bone_breast <dbl>, whole_legs <dbl>,
# thighs <dbl>
dim(eggs_poultry)[1] 120 11
colnames(eggs_poultry) [1] "month" "year" "xlarge_dozen"
[4] "xlarge_half_dozen" "large_dozen" "large_half_dozen"
[7] "whole" "bs_breast" "bone_breast"
[10] "whole_legs" "thighs"
unique(eggs_poultry$year) [1] "2004" "2005" "2006" "2007" "2008" "2009" "2010" "2011" "2012" "2013"
This data set contains 120 rows, 6 columns describing prices of eggs of different sizes and cuts of chicken from 2004 to 2013. There are columns for each different size of egg and cut of chicken, with a corresponding price. This would be much easier to work with if we collapsed these columns into a single column for the egg/poultry type and a separate column for the price. With this, comparisons, averages, and other insights across the different groups or categories will be made much simpler.
The first step in pivoting the data is to try to come up with a concrete vision of what the end product should look like - that way you will know whether or not your pivoting was successful.
One easy way to do this is to think about the dimensions of your current dataset and identify how you want a new one to look.
There are possibly 2 tables here that could be separated, being eggs and chicken but for now we will keep them together. We want to pivot this data longer by creating a new column that will categorize the egg or chicken type and one column for the price. The end result should be 4 columns month, year, type, and price with the amount of rows being increased by a factor of 9.
eggs_poultry_longer <- eggs_poultry %>%
pivot_longer(cols = 3:11 , names_to = "product_type", values_to = "price")
head(eggs_poultry_longer)# A tibble: 6 × 4
month year product_type price
<chr> <chr> <chr> <dbl>
1 Jan 2004 xlarge_dozen 230
2 Jan 2004 xlarge_half_dozen 132
3 Jan 2004 large_dozen 230
4 Jan 2004 large_half_dozen 126
5 Jan 2004 whole 198.
6 Jan 2004 bs_breast 646.
dim(eggs_poultry_longer)[1] 1080 4
As expected the dimensions for the pivoted data now has 1080 rows and 4 columns.