Challenge 3 Solutions

challenge_3
animal_weights
eggs
australian_marriage
usa_households
sce_labor
Tidy Data: Pivoting
Author

Moira Chiong

Published

June 10, 2023

Code
library(tidyverse)
Error: package or namespace load failed for 'tidyverse' in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
 namespace 'magrittr' 2.0.2 is being loaded, but >= 2.0.3 is required
Code
library(tidyr)
library(dplyr)

Challenge Overview

Today’s challenge is to:

  1. read in a data set, and describe the data set using both words and any supporting information (e.g., tables, etc)
  2. identify what needs to be done to tidy the current data
  3. anticipate the shape of pivoted data
  4. pivot the data into tidy format using pivot_longer

Read in one (or more) of the following datasets, using the correct R package and command.

  • animal_weights.csv ⭐
  • eggs_tidy.csv ⭐⭐ or organiceggpoultry.xls ⭐⭐⭐
  • australian_marriage*.xls ⭐⭐⭐
  • USA Households*.xlsx ⭐⭐⭐⭐
  • sce_labor_chart_data_public.xlsx 🌟🌟🌟🌟🌟
Code
## Read in data
library(readr)
Warning: package 'readr' was built under R version 4.1.3
Code
animal_weight <- read_csv("_data/animal_weight.csv")
Rows: 9 Columns: 17
-- Column specification --------------------------------------------------------
Delimiter: ","
chr  (1): IPCC Area
dbl (16): Cattle - dairy, Cattle - non-dairy, Buffaloes, Swine - market, Swi...

i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Code
View(animal_weight)
ncol(animal_weight)
[1] 17
Code
nrow(animal_weight)
[1] 9



Briefly describe the data

Describe the data, and be sure to comment on why you are planning to pivot it to make it “tidy”

This data set is wide, with 17 columns of animals, and 9 rows of observations or geographic areas.

I am planning on pivoting the data because it is so wide with 17 columnns of animals. I am planning on pivoting the data because it will conserve space and the data will be much neater. We will not have to struggle to read in the data once it is pivoted.

Anticipate the End Result

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 data (tibble, dataframe, or matrix), and then calculate what the dimensions of the pivoted data should be.

Suppose you have a dataset with \(n\) rows and \(k\) variables. In our example, 3 of the variables are used to identify a case, so you will be pivoting \(k-3\) variables into a longer format where the \(k-3\) variable names will move into the names_to variable and the current values in each of those columns will move into the values_to variable. Therefore, we would expect \(n * (k-3)\) rows in the pivoted dataframe!

Example: find current and future data dimensions

Lets see if this works with a simple example.

Code
library(tidyr)
animals_tidy <-pivot_longer(animal_weight, col = -`IPCC Area`, names_to = "Livestock", values_to = "Weight")

Or simple example has \(n = 6\) rows and \(k - 3 = 2\) variables being pivoted, so we expect a new dataframe to have \(n * 2 = 12\) rows x \(3 + 2 = 5\) columns.

Challenge: Describe the final dimensions

Document your work here.

Code
ncol(animals_tidy)
[1] 3
Code
nrow(animals_tidy)
[1] 144

Any additional comments?

Pivot the Data

Now we will pivot the data, and compare our pivoted data dimensions to the dimensions calculated above as a “sanity” check.

Example

Code
df<-pivot_longer(df, col = c(outgoing, incoming),
                 names_to="trade_direction",
                 values_to = "trade_value")
Error in UseMethod("pivot_longer"): no applicable method for 'pivot_longer' applied to an object of class "function"
Code
df
function (x, df1, df2, ncp, log = FALSE) 
{
    if (missing(ncp)) 
        .Call(C_df, x, df1, df2, log)
    else .Call(C_dnf, x, df1, df2, ncp, log)
}
<bytecode: 0x000000001eeeb2c8>
<environment: namespace:stats>

Yes, once it is pivoted long, our resulting data are \(12x5\) - exactly what we expected!

Challenge: Pivot the Chosen Data

Document your work here. What will a new “case” be once you have pivoted the data? How does it meet requirements for tidy data?

There are three rules which make a data set tidy:

There are three interrelated rules which make a dataset tidy:

Each variable must have its own column. Each observation must have its own row. Each value must have its own cell.

https://r4ds.had.co.nz/tidy-data.html

This pivoted data set is tidy because each variable has its own column, each observation has its own row and each value has a cell.``

Any additional comments?