10

I have long format data with a structure like this (of course, multiple countries, years, and variables):

df <- tribble(
  ~ind, ~country,    ~year, ~group, ~dummy,    ~v1,    ~v2,
  1,   "country 1",  1990, "A",         0,  2.53,  1.68, 
  2,   "country 1",  1990, "B",         0,  2.63, -5.21, 
  3,   "country 1",  1991, "A",         1,  6.54,  3.48, 
  4,   "country 1",  1991, "B",         1,  7.11,  2.52, 
  5,   "country 1",  1992, "A",         0,  2.69, -3.45, 
  6,   "country 1",  1992, "A",         0,  2.49, -3.45, 
  7,   "country 2",  1990, "A",         0,  2.73,  1.68, 
  8,   "country 2",  1990, "B",         0,  2.73, -1.21, 
  9,   "country 2",  1991, "A",         1,  6.44,  3.28, 
  10,  "country 2",  1991, "B",         1,  3.11,  2.51, 
  11,  "country 2",  1992, "A",         0,  2.64, -3.45, 
  12,  "country 2",  1992, "A",         0,  2.39,  2.85
)

I would like to transform it to wider format, with a structure like this:

ind country    year  A_dummy B_dummy  A_v1 A_v2  B_v1  B_v2  
1   country 1  1990        0       0  2.53 1.68  2.63 -5.21
2   country 2  1990        0       0  2.73 1.68  2.73 -1.21 
3   country 1  1991        1       1  6.54 3.48  7.11  2.52 
4   country 2  1991        1       1  6.44 3.28  3.11  2.51 
# etc.

where each row is one country-year and the columns represent the different variables with a suffix for each group. I think I'll have to use pivot_wider() but couldn't figure out how to preserve the country-year combination.

Can someone point me into the right direction?

Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
user456789
  • 331
  • 1
  • 3
  • 9

2 Answers2

13
df %>%
  pivot_wider(names_from = group,
              values_from = c(dummy, v1, v2))
crestor
  • 1,388
  • 8
  • 21
6

You can pass multiple values in values_from in pivot_wider.

tidyr::pivot_wider(df, names_from = group, values_from = c(dummy, v1, v2))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213