Last active
January 12, 2021 22:36
-
-
Save luisDVA/729de3c1ee03b1912df2299067f92b83 to your computer and use it in GitHub Desktop.
Pivoting Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## %######################################################%## | |
# # | |
#### Pivoting Data - Your Turn #### | |
# # | |
## %######################################################%## | |
# Load the dog ranks data ("dogranks_your.csv") | |
# Pivot the data (wide to long and back to wide) | |
# load packages ----------------------------------------------------------- | |
library(readr) | |
library(tidyr) | |
# import data ------------------------------------------------------------- | |
dogranks <- read_csv("data/dogranks-your.csv") | |
# pivoting ---------------------------------------------------------------- | |
# longer | |
dogranks_long <- dogranks %>% pivot_longer( | |
cols = -c(Breed, Size), | |
names_to = "Year", | |
values_to = "Popularity.Rank" | |
) | |
# back to wide | |
dogranks_wide <- dogranks_long %>% | |
pivot_wider(names_from = "Year", values_from = "Popularity.Rank") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment