======================================
===== Álvaro A. Gutiérrez-Vargas =====
======================================

Reshape using dplyr

Stata R

1 Reshape using dplyr

1.1 A journey from long-land to the wide format town.

Here we simulate data in long format.

# Data in long format
library(dplyr)
set.seed(777)

my.df <- data.frame(id=rep(c("A","B","C"), 5), TIME=rep(1:5, each=3), X=1:15, Y=16:30) %>%
  group_by(id) %>%
  mutate(fix_per_id = rpois(1, lambda = 5))%>%
  arrange(id) 
## # A tibble: 15 x 5
## # Groups:   id [3]
##    id     TIME     X     Y fix_per_id
##    <chr> <int> <int> <int>      <int>
##  1 A         1     1    16          6
##  2 A         2     4    19          6
##  3 A         3     7    22          6
##  4 A         4    10    25          6
##  5 A         5    13    28          6
##  6 B         1     2    17          5
##  7 B         2     5    20          5
##  8 B         3     8    23          5
##  9 B         4    11    26          5
## 10 B         5    14    29          5
## 11 C         1     3    18          4
## 12 C         2     6    21          4
## 13 C         3     9    24          4
## 14 C         4    12    27          4
## 15 C         5    15    30          4

With the followin code we can put it into wide format.

# Data in wide format
my.df %>%
  tidyr::pivot_wider(
    names_from  = c(TIME), # Can accommodate more variables, if needed.
    values_from = c(X, Y)
  )
## # A tibble: 3 x 12
## # Groups:   id [3]
##   id    fix_per_id   X_1   X_2   X_3   X_4   X_5   Y_1   Y_2   Y_3   Y_4   Y_5
##   <chr>      <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 A              6     1     4     7    10    13    16    19    22    25    28
## 2 B              5     2     5     8    11    14    17    20    23    26    29
## 3 C              4     3     6     9    12    15    18    21    24    27    30