Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Last active January 12, 2021 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisDVA/729de3c1ee03b1912df2299067f92b83 to your computer and use it in GitHub Desktop.
Save luisDVA/729de3c1ee03b1912df2299067f92b83 to your computer and use it in GitHub Desktop.
Pivoting Data
## %######################################################%##
# #
#### 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