challenge_3
Author

Akhilesh Kumar Meghwal

Published

August 21, 2022

Code
library(tidyverse)

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

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 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
animal_weight<-read_csv("_data/animal_weight.csv",
                        show_col_types = FALSE)

Briefly describe the data

print(summarytools::dfSummary(hotel_bookings, varnumbers = FALSE, plain.ascii = FALSE, style = “grid”, graph.magnif = 0.70, valid.col = FALSE), method = ‘render’, table.classes = ‘table-condensed’)

  • The data is has 9 rows and 17 columns

  • The dataset contains, weight data of 16 different animal categories across 9 different Intergovernmental Panel on Climate Change (IPCC) Participants.

  • Individual rows don’t represent individual observations

  • And Also all columns contain same weight variable

  • This a good example to apply pivot_longer(), as animal-type column along with animal_weight will be created through pivot_longer, where individual row will represent individual observations for a set of IPCC & animal type

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))

#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

Code
# The pivoted output will be of 12 * 5 dimension, i.e 12 rows and 5 columns and also cases = 1

# number of rows of the pivoted dataset

n_row_pivot = nrow(df) * (ncol(df)-3)

#number of columns of the pivoted dataset

n_col_pivot = (ncol(df)-2)+2

12*5 # 12 rows and 5 columns
[1] 60

Pivot the Data

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

df_pivot<-pivot_longer(df, col = c(‘outgoing’, ‘incoming’), names_to=“trade_direction”, values_to = “trade_value”) df_pivot

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              1332.
 2 Mexico   1980 NAFTA incoming              1044.
 3 USA      1990 NAFTA outgoing              1991.
 4 USA      1990 NAFTA incoming               314.
 5 France   1980 EU    outgoing              1163.
 6 France   1980 EU    incoming              1884.
 7 Mexico   1990 NAFTA outgoing              1931.
 8 Mexico   1990 NAFTA incoming               372.
 9 USA      1980 NAFTA outgoing               977.
10 USA      1980 NAFTA incoming               740.
11 France   1990 EU    outgoing              1685.
12 France   1990 EU    incoming              1156.

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
case =1 

animal_weight_pivot <- pivot_longer(animal_weight, col = names(animal_weight)[2:17], names_to = 'animal_name', values_to = 'weight')

"Now each column is an individual variable and each row is a individual observation, so it can be called a tidy data"
[1] "Now each column is an individual variable and each row is a individual observation, so it can be called a tidy data"
Code
#number of rows in animal_weight dataset = 9
#number of columns in animal_weight dataset = 17
#case = 1
#$n = 9, $k-1 = 16

# pivot_longer output dataset
  #number_of_rows = 9 * 16 = 144
  #number_of_columns = 1 +2 = 3

Any additional comments?