Created
May 29, 2025 10:56
-
-
Save johnmackintosh/2dd4abf509b72ae56a0862231fabab08 to your computer and use it in GitHub Desktop.
RStudio snippets for pivot_longer and pivot_wider
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
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