0

I have something like the following dataframe:


df = structure(list(Month = structure(c(946684800, 946684800, 946684800, 
949363200, 949363200, 949363200, 951868800, 951868800, 951868800
), tzone = "UTC", class = c("POSIXct", "POSIXt")), Country = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("Italy", "Spain", 
"Portugal", "Ireland", "Germany", "France", "Belgium", "Netherlands", 
"Austria", "Finland"), class = "factor"), bucket = c("long", 
"medium", "short", "long", "medium", "short", "long", "medium", 
"short"), Share_bucket = c(0.403418993584752, 0.445804130974895, 
0.150776875440353, 0.416193617674133, 0.458422829088678, 0.125383553237189, 
0.613769196662502, 0.253456406949091, 0.132774396388407)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -9L), groups = structure(list(
    Month = structure(c(946684800, 949363200, 951868800), tzone = "UTC", class = c("POSIXct", 
    "POSIXt")), Country = structure(c(1L, 1L, 1L), levels = c("Italy", 
    "Spain", "Portugal", "Ireland", "Germany", "France", "Belgium", 
    "Netherlands", "Austria", "Finland"), class = "factor"), 
    .rows = structure(list(1:3, 4:6, 7:9), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L), .drop = TRUE))


Month               Country bucket Share_bucket
  <dttm>              <fct>   <chr>         <dbl>
1 2000-01-01 00:00:00 Italy   long          0.403
2 2000-01-01 00:00:00 Italy   medium        0.446
3 2000-01-01 00:00:00 Italy   short         0.151
4 2000-02-01 00:00:00 Italy   long          0.416
5 2000-02-01 00:00:00 Italy   medium        0.458
6 2000-02-01 00:00:00 Italy   short         0.125
7 2000-03-01 00:00:00 Italy   long          0.614
8 2000-03-01 00:00:00 Italy   medium        0.253
9 2000-03-01 00:00:00 Italy   short         0.133


I would like to get the following dataframe:


    Month            Country  short  medium    long    
 
1 2000-01-01 00:00:00 Italy  0.151    0.446   0.403
2 2000-02-01 00:00:00 Italy  0.125    0.458   0.416
3 2000-03-01 00:00:00 Italy  0.133    0.253   0.614

I have been trying without success with pivot_wider.

Can anyone help me?

Thanks!

Rollo99
  • 1,601
  • 7
  • 15

1 Answers1

1

Here you are:

df %>% 
  pivot_wider(
    names_from = bucket,
    values_from = Share_bucket
  )
# A tibble: 3 × 5
# Groups:   Month, Country [3]
  Month               Country  long medium short
  <dttm>              <fct>   <dbl>  <dbl> <dbl>
1 2000-01-01 00:00:00 Italy   0.403  0.446 0.151
2 2000-02-01 00:00:00 Italy   0.416  0.458 0.125
3 2000-03-01 00:00:00 Italy   0.614  0.253 0.133
Josh White
  • 1,003
  • 1
  • 17
  • Is the ordering important? `pivot_wider()` by default puts the columns in the order they appear in the data, so if you want the order different, you can just change the order in the dataframe before you pivot. Or of course, it might just be easier to `relocate()` the columns afterwards. – Josh White Nov 15 '22 at 12:07