Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Created January 5, 2018 05:20
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 Ram-N/1e5509ed448cae77e3e3e0dfb0882e16 to your computer and use it in GitHub Desktop.
Save Ram-N/1e5509ed448cae77e3e3e0dfb0882e16 to your computer and use it in GitHub Desktop.
TIDYR - gather, unite and spread.
library(tidyr)
df <- data.frame(
MKT=c("ABIORD", "ABIBOS", "GCFPQR", "ABIORD", "ABIBOS", "GCFPQR", "ABIORD", "ABIBOS", "GCFPQR"),
VERSION=c(1,2,3,4,1,2,3,4,1),
REVERSALS=c(7,6,7,7,7,6,7,7, 9),
NEIGH=c(1,2.3,3.3,4,3.3,2,1,3.3,4.3),
DIFFMETRIC=c(1.1,2.1,3.1,4.1,3.1,2.1,1.1,3.1,4.1))
df %>% gather("key", "value", 3:5) %>% # long format, leaving just MKT and VERSION as row identifiers
unite(new, VERSION, key) %>% #create new combined col_names
spread(key = new, value = value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment