top of page

Reshaping Data

Updated: Jan 27, 2021

Basics

- Wide, or unstacked data is presented with each different data variable in a separate column.

- Narrow, stacked, or long data is presented with one column containing all the values and another column listing the context of the value.


To create a wide data

df_wide <- data.frame(Person = c("Bob","Alice","Steve","Jack"),
                      Age =  c(32,24,64, 55),
                      Weight= c (168,150,144,180),
                      Height =  c(180,175,165,176))

df_wide 
##   Person Age Weight Height
## 1    Bob  32    168    180
## 2  Alice  24    150    175
## 3  Steve  64    144    165
## 4   Jack  55    180    176

A. Converting wide data to long format

library(tidyr)
df_long <- df_wide %>% 
  pivot_longer(cols = c("Age","Weight","Height"), 
               names_to = "type", 
               values_to = "value")

df_long
## # A tibble: 12 x 3
##    Person type   value
##    <fct>  <chr>  <dbl>
##  1 Bob    Age       32
##  2 Bob    Weight   168
##  3 Bob    Height   180
##  4 Alice  Age       24
##  5 Alice  Weight   150
##  6 Alice  Height   175
##  7 Steve  Age       64
##  8 Steve  Weight   144
##  9 Steve  Height   165
## 10 Jack   Age       55
## 11 Jack   Weight   180
## 12 Jack   Height   176

B. Converting the wide data to long data

to_df_wide <- df_long %>% 
  pivot_wider(names_from = type, 
              values_from = value) 

to_df_wide 
## # A tibble: 4 x 4
##   Person   Age Weight Height
##   <fct>  <dbl>  <dbl>  <dbl>
## 1 Bob       32    168    180
## 2 Alice     24    150    175
## 3 Steve     64    144    165
## 4 Jack      55    180    176

Recent Posts

See All

Comentários


bottom of page