Code
library(tidyverse)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Xiaoyan Hu
September 27, 2022
Today’s challenge is to:
pivot_longer
Read in one (or more) of the following datasets, using the correct R package and command.
Error in setwd("/Users/cassie199/Desktop/22fall/DACSS601/601_Fall_2022/posts/_data"): cannot change working directory
Error: 'eggs_tidy.csv' does not exist in current working directory ('C:/Users/srika/OneDrive/Desktop/601_Fall_2022/posts').
Error in head(data1): object 'data1' not found
Describe the data, and be sure to comment on why you are planning to pivot it to make it “tidy” – in eggs tidy file, there are a lot different animals including:
some of them are not necessary such as cattles - dairy and non dairy since they are both cattles. Similar as swine, chicken. They can be combined together to show less columns.
In this file there is 120 varibles and 6 observations.This data including 10 years data from each month and recorded four different sizes of egg (sold or produced).
some of the varibles seems unnecessary such as repeated in years or month, they can be combine to one column. Therefore, this data is great for pivot. My expected result is to see in each month(by colomn), how many eggs in each size were produced/or sold
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 677. 462.
2 USA 1990 NAFTA 835. -453.
3 France 1980 EU 215. 1392.
4 Mexico 1990 NAFTA 1317. 957.
5 USA 1980 NAFTA 1373. 407.
6 France 1990 EU 116. 1478.
[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.
Error in eval(expr, envir, enclos): object 'data1' not found
Error in nrow(data1): object 'data1' not found
Error in ncol(data1): object 'data1' not found
Error in nrow(data1): object 'data1' not found
[1] 4
Error in nrow(data1): object 'data1' not found
[1] 14
Any additional comments? I dont quiet understand how did you get 3 in here since income and outcome considered as 2.
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 677.
2 Mexico 1980 NAFTA incoming 462.
3 USA 1990 NAFTA outgoing 835.
4 USA 1990 NAFTA incoming -453.
5 France 1980 EU outgoing 215.
6 France 1980 EU incoming 1392.
7 Mexico 1990 NAFTA outgoing 1317.
8 Mexico 1990 NAFTA incoming 957.
9 USA 1980 NAFTA outgoing 1373.
10 USA 1980 NAFTA incoming 407.
11 France 1990 EU outgoing 116.
12 France 1990 EU incoming 1478.
Yes, once it is pivoted long, our resulting data are \(12x5\) - exactly what we expected!
Document your work here. What will a new “case” be once you have pivoted the data? How does it meet requirements for tidy data?
Error in pivot_longer(data1, col = c(large_half_dozen, large_dozen, extra_large_half_dozen, : object 'data1' not found
Error in eval(expr, envir, enclos): object 'data1' not found
Error in pivot_wider(data1, names_from = "month", values_from = "count"): object 'data1' not found
Error in eval(expr, envir, enclos): object 'data1' not found
Any additional comments??
---
title: "Challenge 3 Xiaoyan Hu"
author: "Xiaoyan Hu"
desription: "Tidy Data: Pivoting"
date: "09/27/2022"
format:
html:
toc: true
code-fold: true
code-copy: true
code-tools: true
categories:
- challenge_3
- animal_weights
- eggs
- australian_marriage
- usa_households
- sce_labor
---
```{r}
#| label: setup
#| warning: false
#| message: false
library(tidyverse)
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)
```
## {.tabset}
### 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 and describe the data
#### {.tabset}
##### 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 🌟🌟🌟🌟🌟
```{r}
# read the data and preview
setwd("/Users/cassie199/Desktop/22fall/DACSS601/601_Fall_2022/posts/_data")
data1<-read_csv("eggs_tidy.csv")
#preview
head(data1)
```
##### Briefly describe the data.
Describe the data, and be sure to comment on why you are planning to pivot it to make it "tidy"
-- in eggs tidy file, there are a lot different animals including:
some of them are not necessary such as cattles - dairy and non dairy since they are both cattles. Similar as swine, chicken. They can be combined together to show less columns.
In this file there is 120 varibles and 6 observations.This data including 10 years data from each month and recorded four different sizes of egg (sold or produced).
some of the varibles seems unnecessary such as repeated in years or month, they can be combine to one column. Therefore, this data is great for pivot.
My expected result is to see in each month(by colomn), how many eggs in each size were produced/or sold
```{r}
#dimension of data
dim(data1)
#column names
colnames(data1)
```
### Anticipate the End Result and examples
#### {.tabset}
##### Instructions
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.
```{r}
#current dimension
dim(data1)
# existing rows
nrow(data1)
# existing columns
ncol(data1)
#expected rows from first pivot
nrow(data1) * (ncol(data1)-2)
# expected column from first pivot
6-2
#expected rows from second pivot
(nrow(data1) * (ncol(data1)-2)) /12
# expected column from first pivot
2+12
```
Any additional comments?
I dont quiet understand how did you get 3 in here since income and outcome considered as 2.
### Pivot the Data
#### {.tabset}
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
Document your work here. What will a new "case" be once you have pivoted the data? How does it meet requirements for tidy data?
```{r}
#pivot rows
data1<-pivot_longer(data1, col = c(large_half_dozen, large_dozen, extra_large_half_dozen, extra_large_dozen), names_to = "size", values_to = "count")
data1
data1<-pivot_wider(data1, names_from = "month", values_from = "count")
data1
```
Any additional comments??