challenge_3
animal_weights
eggs
australian_marriage
usa_households
sce_labor
Author

Janani Natarajan

Published

May 8, 2023

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 organiceggpoultry.xls ⭐⭐⭐
  • australian_marriage*.xls ⭐⭐⭐
  • USA Households*.xlsx ⭐⭐⭐⭐
  • sce_labor_chart_data_public.xlsx 🌟🌟🌟🌟🌟
Code
animals <- read.csv("_data/animal_weight.csv")

Briefly describe the data

Code
head(animals)
            IPCC.Area Cattle...dairy Cattle...non.dairy Buffaloes
1 Indian Subcontinent            275                110       295
2      Eastern Europe            550                391       380
3              Africa            275                173       380
4             Oceania            500                330       380
5      Western Europe            600                420       380
6       Latin America            400                305       380
  Swine...market Swine...breeding Chicken...Broilers Chicken...Layers Ducks
1             28               28                0.9              1.8   2.7
2             50              180                0.9              1.8   2.7
3             28               28                0.9              1.8   2.7
4             45              180                0.9              1.8   2.7
5             50              198                0.9              1.8   2.7
6             28               28                0.9              1.8   2.7
  Turkeys Sheep Goats Horses Asses Mules Camels Llamas
1     6.8  28.0  30.0    238   130   130    217    217
2     6.8  48.5  38.5    377   130   130    217    217
3     6.8  28.0  30.0    238   130   130    217    217
4     6.8  48.5  38.5    377   130   130    217    217
5     6.8  48.5  38.5    377   130   130    217    217
6     6.8  28.0  30.0    238   130   130    217    217
Code
summarize(animals)
data frame with 0 columns and 1 row
Code
view(animals)
ncol(animals)
[1] 17
Code
nrow(animal_weight)
Error in nrow(animal_weight): object 'animal_weight' not found

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!

Finding current and future data dimensions

Code
nrow(animals)
[1] 9
Code
ncol(animals)
[1] 17
Code
nrow(animals) * (ncol(animals)-1)
[1] 144

Pivot the Data

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

Code
animals2 <- pivot_longer(animals, `Cattle...dairy`:`Llamas`, names_to = "type", values_to = "weights")

Describe the final dimensions

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