Code
library(tidyverse)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Emma Rasmussen
August 17, 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`
# ℹ Use `colnames()` to see all variable names
Describe the data, and be sure to comment on why you are planning to pivot it to make it “tidy”
The data appears to illustrate (average?) animal weights by region. To tidy the data we will pivot the columns with animal names into a single column. Each “case” is an animal type within a region, and the values/dependent variable is the weight.
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 376. 2703.
2 USA 1990 NAFTA 1543. 628.
3 France 1980 EU 1231. 383.
4 Mexico 1990 NAFTA 669. 779.
5 USA 1980 NAFTA 887. 1696.
6 France 1990 EU 911. 1937.
[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.
Document your work here.
OG Dataset has k=17 columns and n=9 rows. 17-1(1 existing variable to describe each case (country), the other 16 columns need to be pivoted) We wll now have three columns, one region, one animalm(new col) (together the IV), one weight(the DV) (new col) 9*16 rows expected in data frame= 144 3col byt 144 rows rows expected
[1] 9
[1] 17
[1] 144
[1] 3
Any additional comments? See comment at bottom
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 376.
2 Mexico 1980 NAFTA incoming 2703.
3 USA 1990 NAFTA outgoing 1543.
4 USA 1990 NAFTA incoming 628.
5 France 1980 EU outgoing 1231.
6 France 1980 EU incoming 383.
7 Mexico 1990 NAFTA outgoing 669.
8 Mexico 1990 NAFTA incoming 779.
9 USA 1980 NAFTA outgoing 887.
10 USA 1980 NAFTA incoming 1696.
11 France 1990 EU outgoing 911.
12 France 1990 EU incoming 1937.
Yes, once it is pivoted long, our resulting data are \(12x5\) - exactly what we expected!
A case will be an animal from a particular region. It meets the requirements for tidy data because each case has its own row, and each variable has its own column.
# A tibble: 144 × 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
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
# ℹ Use `print(n = ...)` to see more rows
Any additional comments?
I am a little confused by the calculation, I tried to work out everything above in a way that made sense. But we essentially start with: - how many columns will remain unpivoted (variables that start in the correct place/column) -how many columns are being pivoted (the rest or starting number of col minus number above) -The number of columns being pivoted*number of rows = new number of rows -the new number of columns is the unchanged columns plus 1 for the variables contained in the pivot plus one for the values?? So unchanged col+2
---
title: "Challenge 3"
author: "Emma Rasmussen"
desription: "Tidy Data: Pivoting"
date: "08/17/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 ⭐
- eggs_tidy.csv ⭐⭐ or organicpoultry.xls ⭐⭐⭐
- australian_marriage\*.xlsx ⭐⭐⭐
- USA Households\*.xlsx ⭐⭐⭐⭐
- sce_labor_chart_data_public.csv 🌟🌟🌟🌟🌟
```{r}
animal_weight<-read_csv("_data/animal_weight.csv",
show_col_types = FALSE)
animal_weightOG<-animal_weight#saving a copy of the original data set
animal_weight
```
### Briefly describe the data
Describe the data, and be sure to comment on why you are planning to pivot it to make it "tidy"
The data appears to illustrate (average?) animal weights by region. To tidy the data we will pivot the columns with animal names into a single column. Each "case" is an animal type within a region, and the values/dependent variable is the weight.
## 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
Document your work here.
OG Dataset has k=17 columns and n=9 rows.
17-1(1 existing variable to describe each case (country), the other 16 columns need to be pivoted)
We wll now have three columns, one region, one animalm(new col) (together the IV), one weight(the DV) (new col)
9*16 rows expected in data frame= 144
3col byt 144 rows rows expected
```{r}
nrow(animal_weightOG)
ncol(animal_weightOG)
#expected rows/cases
nrow(animal_weightOG)*(ncol(animal_weightOG)-1)
#expected columns
1+2
```
Any additional comments?
See comment at bottom
## 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
A case will be an animal from a particular region. It meets the requirements for tidy data because each case has its own row, and each variable has its own column.
```{r}
pivot_longer(animal_weight, col = c(2:17),
names_to="animal_type",
values_to = "weight")
```
Any additional comments?
I am a little confused by the calculation, I tried to work out everything above in a way that made sense. But we essentially start with:
- how many columns will remain unpivoted (variables that start in the correct place/column)
-how many columns are being pivoted (the rest or starting number of col minus number above)
-The number of columns being pivoted*number of rows = new number of rows
-the new number of columns is the unchanged columns plus 1 for the variables contained in the pivot plus one for the values?? So unchanged col+2