# A tibble: 6 × 4
country continent var_year value
<fct> <fct> <chr> <dbl>
1 Afghanistan Asia lifeExp_1952 28.8
2 Afghanistan Asia lifeExp_1957 30.3
3 Afghanistan Asia lifeExp_1962 32.0
4 Afghanistan Asia lifeExp_1967 34.0
5 Afghanistan Asia lifeExp_1972 36.1
6 Afghanistan Asia lifeExp_1977 38.4
var_year contains both variable and year information
value has the actual number value
Many rows!
4 Step 2: Split var_year into two variables with separate()
gap_tall2 <- gap_tall %>%separate(var_year, sep ="_", into =c("variable", "year"))head(gap_tall2)
# A tibble: 6 × 5
country continent variable year value
<fct> <fct> <chr> <chr> <dbl>
1 Afghanistan Asia lifeExp 1952 28.8
2 Afghanistan Asia lifeExp 1957 30.3
3 Afghanistan Asia lifeExp 1962 32.0
4 Afghanistan Asia lifeExp 1967 34.0
5 Afghanistan Asia lifeExp 1972 36.1
6 Afghanistan Asia lifeExp 1977 38.4
Still many rows, but the variable name and year are split into 2 variables now
5 Step 3: Finish converting back to tall data with another pivot_wider()
Weird to have to use pivot_wider() to get back to the tall dataset…
# A tibble: 6 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <chr> <dbl> <dbl> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
3 Afghanistan Asia 1962 32.0 10267083 853.
4 Afghanistan Asia 1967 34.0 11537966 836.
5 Afghanistan Asia 1972 36.1 13079460 740.
6 Afghanistan Asia 1977 38.4 14880372 786.
6 Compare to original data
head(gapminder)
# A tibble: 6 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
3 Afghanistan Asia 1962 32.0 10267083 853.
4 Afghanistan Asia 1967 34.0 11537966 836.
5 Afghanistan Asia 1972 36.1 13079460 740.
6 Afghanistan Asia 1977 38.4 14880372 786.
head(gap_tall3)
# A tibble: 6 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <chr> <dbl> <dbl> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
3 Afghanistan Asia 1962 32.0 10267083 853.
4 Afghanistan Asia 1967 34.0 11537966 836.
5 Afghanistan Asia 1972 36.1 13079460 740.
6 Afghanistan Asia 1977 38.4 14880372 786.
Source Code
---title: "Pivot wider then reverse back to tall data"format: html: self-contained: true self-contained-math: true html-math-method: katex number-sections: true toc: true code-tools: true code-block-bg: true code-block-border-left: "#31BAE9"---```{r}#| label: setuplibrary(gapminder)library(tidyverse)```## Read in and view data```{r}data(gapminder)head(gapminder)```## Full wide dataset with `pivot_wider()````{r}gap_wide <- gapminder %>%pivot_wider(names_from = year, values_from =c(lifeExp, pop, gdpPercap))head(gap_wide)```* One column per variable (`lifeExp`, `pop`, `gdpPercap`) per `year`## Step 1: Reverse part way with `pivot_longer()````{r}gap_tall <- gap_wide %>%pivot_longer(cols =3:38, names_to ="var_year", values_to ="value")head(gap_tall) ```* `var_year` contains both variable and year information* `value` has the actual number value* Many rows!## Step 2: Split `var_year` into two variables with `separate()````{r}gap_tall2 <- gap_tall %>%separate(var_year, sep ="_", into =c("variable", "year"))head(gap_tall2)```* Still many rows, but the variable name and year are split into 2 variables now## Step 3: Finish converting back to tall data with another `pivot_wider()`* Weird to have to use `pivot_wider()` to get back to the tall dataset...```{r}gap_tall3 <- gap_tall2 %>%pivot_wider(names_from = variable,values_from = value)head(gap_tall3)```## Compare to original data```{r}head(gapminder) head(gap_tall3)```