challenge_3
Author

Pavan Datta Abbineni

Published

August 17, 2022

Code
library(tidyverse)

knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)

Read in data

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

  • animal_weights.csv ⭐
  • eggs_tidy.csv ⭐⭐ or organicpoultry.xls ⭐⭐⭐
  • australian_marriage*.xlsx ⭐⭐⭐
  • USA Households*.xlsx ⭐⭐⭐⭐
  • sce_labor_chart_data_public.csv 🌟🌟🌟🌟🌟
Code
animalWeights<-read_csv("_data/animal_weight.csv",
                        show_col_types = FALSE)
Code
head(animalWeights)
# A tibble: 6 × 17
  IPCC A…¹ Cattl…² Cattl…³ Buffa…⁴ Swine…⁵ Swine…⁶ Chick…⁷ Chick…⁸ Ducks Turkeys
  <chr>      <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <dbl>   <dbl>
1 Indian …     275     110     295      28      28     0.9     1.8   2.7     6.8
2 Eastern…     550     391     380      50     180     0.9     1.8   2.7     6.8
3 Africa       275     173     380      28      28     0.9     1.8   2.7     6.8
4 Oceania      500     330     380      45     180     0.9     1.8   2.7     6.8
5 Western…     600     420     380      50     198     0.9     1.8   2.7     6.8
6 Latin A…     400     305     380      28      28     0.9     1.8   2.7     6.8
# … with 7 more variables: Sheep <dbl>, Goats <dbl>, Horses <dbl>, Asses <dbl>,
#   Mules <dbl>, Camels <dbl>, Llamas <dbl>, and abbreviated variable names
#   ¹​`IPCC Area`, ²​`Cattle - dairy`, ³​`Cattle - non-dairy`, ⁴​Buffaloes,
#   ⁵​`Swine - market`, ⁶​`Swine - breeding`, ⁷​`Chicken - Broilers`,
#   ⁸​`Chicken - Layers`
# ℹ Use `colnames()` to see all variable names
Code
dim(animalWeights)
[1]  9 17
Code
colnames(animalWeights)
 [1] "IPCC Area"          "Cattle - dairy"     "Cattle - non-dairy"
 [4] "Buffaloes"          "Swine - market"     "Swine - breeding"  
 [7] "Chicken - Broilers" "Chicken - Layers"   "Ducks"             
[10] "Turkeys"            "Sheep"              "Goats"             
[13] "Horses"             "Asses"              "Mules"             
[16] "Camels"             "Llamas"            

Briefly describe the data

The data shows average animal weights per location. We will combine the animal name columns into one column to clean up the data.

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
df<-tibble(country = rep(c("Mexico", "USA", "France"),2),
           year = rep(c(1980,1990), 3), 
           trade = rep(c("NAFTA", "NAFTA", "EU"),2),
           outgoing = rnorm(6, mean=1000, sd=500),
           incoming = rlogis(6, location=1000, 
                             scale = 400))
df
# A tibble: 6 × 5
  country  year trade outgoing incoming
  <chr>   <dbl> <chr>    <dbl>    <dbl>
1 Mexico   1980 NAFTA     464.     483.
2 USA      1990 NAFTA     972.    1156.
3 France   1980 EU       -110.    -202.
4 Mexico   1990 NAFTA    2185.    1460.
5 USA      1980 NAFTA     717.    1325.
6 France   1990 EU       1338.    1212.
Code
#existing rows/cases
nrow(df)
[1] 6
Code
#existing columns/cases
ncol(df)
[1] 5
Code
#expected rows/cases
nrow(df) * (ncol(df)-3)
[1] 12
Code
# expected columns 
3 + 2
[1] 5

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

Our original dataset has a total of 9 rows and 17 columns.
Out of these 17 columns except the country column the other 16 columns need to be pivoted. In the new dataset the number of rows in the expected dataset is 9*16. New dataset is 144x3.

Code
nrow(animalWeights)
[1] 9
Code
ncol(animalWeights)
[1] 17
Code
nrow(animalWeights)*(ncol(animalWeights)-1)
[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")
df
# A tibble: 12 × 5
   country  year trade trade_direction trade_value
   <chr>   <dbl> <chr> <chr>                 <dbl>
 1 Mexico   1980 NAFTA outgoing               464.
 2 Mexico   1980 NAFTA incoming               483.
 3 USA      1990 NAFTA outgoing               972.
 4 USA      1990 NAFTA incoming              1156.
 5 France   1980 EU    outgoing              -110.
 6 France   1980 EU    incoming              -202.
 7 Mexico   1990 NAFTA outgoing              2185.
 8 Mexico   1990 NAFTA incoming              1460.
 9 USA      1980 NAFTA outgoing               717.
10 USA      1980 NAFTA incoming              1325.
11 France   1990 EU    outgoing              1338.
12 France   1990 EU    incoming              1212.

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?

Code
newPivotedData<-pivot_longer(animalWeights, col = c(2:17),
                 names_to="animal_type",
                 values_to = "weight")
Code
head(newPivotedData)
# A tibble: 6 × 3
  `IPCC Area`         animal_type        weight
  <chr>               <chr>               <dbl>
1 Indian Subcontinent Cattle - dairy      275  
2 Indian Subcontinent Cattle - non-dairy  110  
3 Indian Subcontinent Buffaloes           295  
4 Indian Subcontinent Swine - market       28  
5 Indian Subcontinent Swine - breeding     28  
6 Indian Subcontinent Chicken - Broilers    0.9
Code
dim(newPivotedData)
[1] 144   3

Any additional comments?

The expected dimensions of our pivoted data aligns with our experimentally calculated value.