First we will call the Tidyverse library and create an example dataset.
Let’s get to know the dataset.
head(df1)
## # A tibble: 6 x 3
## # Groups: Year [2]
## Year Color Score
## <dbl> <fct> <dbl>
## 1 2000 blue 218
## 2 2000 green 229
## 3 2000 orange 226
## 4 2000 red 219
## 5 2000 yellow 182
## 6 2001 blue 165
names(df1)
## [1] "Year" "Color" "Score"
n_distinct(df1$Color)
## [1] 5
Now we will take the Color column and make each color its own colomn.
wide <- df1 %>% pivot_wider(names_from = Color, values_from = Score)
print(wide)
## # A tibble: 5 x 6
## # Groups: Year [5]
## Year blue green orange red yellow
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2000 218 229 226 219 182
## 2 2001 165 149 151 273 160
## 3 2002 134 198 187 130 307
## 4 2003 143 194 236 189 216
## 5 2004 153 322 227 182 299
Now we will use pivot longer to change the wide dataset back to its original format
long <- wide %>% pivot_longer(-Year, names_to = "Color", values_to = "Score" )
print(long)
## # A tibble: 25 x 3
## # Groups: Year [5]
## Year Color Score
## <dbl> <chr> <dbl>
## 1 2000 blue 218
## 2 2000 green 229
## 3 2000 orange 226
## 4 2000 red 219
## 5 2000 yellow 182
## 6 2001 blue 165
## 7 2001 green 149
## 8 2001 orange 151
## 9 2001 red 273
## 10 2001 yellow 160
## # … with 15 more rows
Let’s take it a step further and widen out both the year and the color.
wide2 <- df1 %>% pivot_wider(names_from = c(Year, Color), values_from = Score)
print(wide2)
## # A tibble: 1 x 25
## `2000_blue` `2000_green` `2000_orange` `2000_red` `2000_yellow` `2001_blue`
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 218 229 226 219 182 165
## # … with 19 more variables: `2001_green` <dbl>, `2001_orange` <dbl>,
## # `2001_red` <dbl>, `2001_yellow` <dbl>, `2002_blue` <dbl>,
## # `2002_green` <dbl>, `2002_orange` <dbl>, `2002_red` <dbl>,
## # `2002_yellow` <dbl>, `2003_blue` <dbl>, `2003_green` <dbl>,
## # `2003_orange` <dbl>, `2003_red` <dbl>, `2003_yellow` <dbl>,
## # `2004_blue` <dbl>, `2004_green` <dbl>, `2004_orange` <dbl>,
## # `2004_red` <dbl>, `2004_yellow` <dbl>
Now we will take the previous dataset (wide2) and revert it back to it’s original format
long2 <- wide2 %>% pivot_longer(everything(), names_sep = "_", names_to = c("Year","Color"), values_to = "Score" )
print(long2)
## # A tibble: 25 x 3
## Year Color Score
## <chr> <chr> <dbl>
## 1 2000 blue 218
## 2 2000 green 229
## 3 2000 orange 226
## 4 2000 red 219
## 5 2000 yellow 182
## 6 2001 blue 165
## 7 2001 green 149
## 8 2001 orange 151
## 9 2001 red 273
## 10 2001 yellow 160
## # … with 15 more rows