Skip to content

Instantly share code, notes, and snippets.

@johnmackintosh
Created May 29, 2025 10:56
Show Gist options
  • Save johnmackintosh/2dd4abf509b72ae56a0862231fabab08 to your computer and use it in GitHub Desktop.
Save johnmackintosh/2dd4abf509b72ae56a0862231fabab08 to your computer and use it in GitHub Desktop.
RStudio snippets for pivot_longer and pivot_wider
pivot_longer(mydf,
cols = columns to pivot long,
names_to = "desired name for category column",
values_to = "desired name for value column"
)
pivot_wider(mydf,
# id_cols = optional vector of unaffected columns,
names_from = c(category column(s) to pivot wide),
values_from = c(value column(s) that hold data for each category column),
names_sep = "_"
)
# source: unknown..might have been me, might have been copied from somewhere else
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment