Code
library(tidyverse)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Tracy Tien
December 1, 2022
Today’s challenge is to:
pivot_longer
Read in one (or more) of the following datasets, using the correct R package and command.
# A tibble: 9 × 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
7 Asia 350 391 380 50 180 0.9 1.8 2.7 6.8
8 Middle … 275 173 380 28 28 0.9 1.8 2.7 6.8
9 Norther… 604 389 380 46 198 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`
cols(
`IPCC Area` = col_character(),
`Cattle - dairy` = col_double(),
`Cattle - non-dairy` = col_double(),
Buffaloes = col_double(),
`Swine - market` = col_double(),
`Swine - breeding` = col_double(),
`Chicken - Broilers` = col_double(),
`Chicken - Layers` = col_double(),
Ducks = col_double(),
Turkeys = col_double(),
Sheep = col_double(),
Goats = col_double(),
Horses = col_double(),
Asses = col_double(),
Mules = col_double(),
Camels = col_double(),
Llamas = col_double()
)
vars n mean sd median trimmed mad min max range
IPCC Area* 1 9 5.00 2.74 5.0 5.00 2.97 1.0 9.0 8.0
Cattle - dairy 2 9 425.44 140.39 400.0 425.44 185.32 275.0 604.0 329.0
Cattle - non-dairy 3 9 298.00 116.26 330.0 298.00 90.44 110.0 420.0 310.0
Buffaloes 4 9 370.56 28.33 380.0 370.56 0.00 295.0 380.0 85.0
Swine - market 5 9 39.22 10.79 45.0 39.22 7.41 28.0 50.0 22.0
Swine - breeding 6 9 116.44 84.19 180.0 116.44 26.69 28.0 198.0 170.0
Chicken - Broilers 7 9 0.90 0.00 0.9 0.90 0.00 0.9 0.9 0.0
Chicken - Layers 8 9 1.80 0.00 1.8 1.80 0.00 1.8 1.8 0.0
Ducks 9 9 2.70 0.00 2.7 2.70 0.00 2.7 2.7 0.0
Turkeys 10 9 6.80 0.00 6.8 6.80 0.00 6.8 6.8 0.0
Sheep 11 9 39.39 10.80 48.5 39.39 0.00 28.0 48.5 20.5
Goats 12 9 34.72 4.48 38.5 34.72 0.00 30.0 38.5 8.5
Horses 13 9 315.22 73.26 377.0 315.22 0.00 238.0 377.0 139.0
Asses 14 9 130.00 0.00 130.0 130.00 0.00 130.0 130.0 0.0
Mules 15 9 130.00 0.00 130.0 130.00 0.00 130.0 130.0 0.0
Camels 16 9 217.00 0.00 217.0 217.00 0.00 217.0 217.0 0.0
Llamas 17 9 217.00 0.00 217.0 217.00 0.00 217.0 217.0 0.0
skew kurtosis se
IPCC Area* 0.00 -1.60 0.91
Cattle - dairy 0.11 -1.92 46.80
Cattle - non-dairy -0.45 -1.68 38.75
Buffaloes -2.07 2.63 9.44
Swine - market -0.12 -2.12 3.60
Swine - breeding -0.17 -2.15 28.06
Chicken - Broilers NaN NaN 0.00
Chicken - Layers NaN NaN 0.00
Ducks NaN NaN 0.00
Turkeys NaN NaN 0.00
Sheep -0.19 -2.17 3.60
Goats -0.19 -2.17 1.49
Horses -0.19 -2.17 24.42
Asses NaN NaN 0.00
Mules NaN NaN 0.00
Camels NaN NaN 0.00
Llamas NaN NaN 0.00
The animal_weight
dataset contains 16 types (including subtypes, such as dairy cattle vs. non-dairy cattle) of livestock weight information for 9 IPCC (Intergovernmental Panel on Climate Change) areas.
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!
Lets see if this works with a simple example.
# A tibble: 6 × 5
country year trade outgoing incoming
<chr> <dbl> <chr> <dbl> <dbl>
1 Mexico 1980 NAFTA 1007. 896.
2 USA 1990 NAFTA 501. 526.
3 France 1980 EU 1268. 1085.
4 Mexico 1990 NAFTA 748. 357.
5 USA 1980 NAFTA 1022. 1191.
6 France 1990 EU 1215. 2224.
[1] 6
[1] 5
[1] 12
[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.
The current dimensions of animal_weight
are 9 rows of IPCC Areas, and 17 columns (types of livestock). 9*(17-1)=144 rows, and we will want each row to have the IPCC region, type of livestock, and the weight. This means there should be 3 columns.
[1] 9
[1] 17
[1] 144
Any additional comments?
Now we will pivot the data, and compare our pivoted data dimensions to the dimensions calculated above as a “sanity” check.
# A tibble: 12 × 5
country year trade trade_direction trade_value
<chr> <dbl> <chr> <chr> <dbl>
1 Mexico 1980 NAFTA outgoing 1007.
2 Mexico 1980 NAFTA incoming 896.
3 USA 1990 NAFTA outgoing 501.
4 USA 1990 NAFTA incoming 526.
5 France 1980 EU outgoing 1268.
6 France 1980 EU incoming 1085.
7 Mexico 1990 NAFTA outgoing 748.
8 Mexico 1990 NAFTA incoming 357.
9 USA 1980 NAFTA outgoing 1022.
10 USA 1980 NAFTA incoming 1191.
11 France 1990 EU outgoing 1215.
12 France 1990 EU incoming 2224.
Yes, once it is pivoted long, our resulting data are \(12x5\) - exactly what we expected!
Using pivot_longer
to transform the data where each row represents a livestock type with its weight within an IPCC region.
# A tibble: 144 × 3
`IPCC Area` `livestock 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
7 Indian Subcontinent Chicken - Layers 1.8
8 Indian Subcontinent Ducks 2.7
9 Indian Subcontinent Turkeys 6.8
10 Indian Subcontinent Sheep 28
# … with 134 more rows
---
title: "Challenge 3"
author: "Tracy Tien"
desription: "Tidy Data: Pivoting"
date: "12/01/2022"
format:
html:
toc: true
code-fold: true
code-copy: true
code-tools: true
categories:
- challenge_3
- animal_weights
---
```{r}
#| label: setup
#| warning: false
#| message: false
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 ⭐
```{r}
#Read in the data
animal_weight <- read_csv("_data/animal_weight.csv")
animal_weight
spec(animal_weight)
#Load packages
library(tidyverse)
#install.packages("psych")
library(psych)
describe(animal_weight)
```
### Briefly describe the data
The `animal_weight` dataset contains 16 types (including subtypes, such as dairy cattle vs. non-dairy cattle) of livestock weight information for 9 IPCC (Intergovernmental Panel on Climate Change) areas.
## 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.
```{r}
#| tbl-cap: Example
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
#existing rows/cases
nrow(df)
#existing columns/cases
ncol(df)
#expected rows/cases
nrow(df) * (ncol(df)-3)
# expected columns
3 + 2
```
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
The current dimensions of `animal_weight` are 9 rows of IPCC Areas, and 17 columns (types of livestock). 9*(17-1)=144 rows, and we will want each row to have the IPCC region, type of livestock, and the weight. This means there should be 3 columns.
```{r}
#Existing rows/cases
nrow(animal_weight)
#Existing columns/cases
ncol(animal_weight)
#Expected rows/cases
nrow(animal_weight) * (ncol(animal_weight)-1) #subtract first column IPCC Area
```
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
```{r}
#| tbl-cap: Pivoted Example
df<-pivot_longer(df, col = c(outgoing, incoming),
names_to="trade_direction",
values_to = "trade_value")
df
```
Yes, once it is pivoted long, our resulting data are $12x5$ - exactly what we expected!
### Challenge: Pivot the Chosen Data
Using `pivot_longer` to transform the data where each row represents a livestock type with its weight within an IPCC region.
```{r}
animal_weight_pivot_longer <- pivot_longer(animal_weight,
cols = -`IPCC Area`, #the - means except for this column?
names_to = "livestock type",
values_to = "weight")
animal_weight_pivot_longer
```