Home > database >  How to use pivot_longer in this case?
How to use pivot_longer in this case?

Time:05-07

I have the following data frame:

df =structure(list(Country = c("DE", "DE", "DE", "DE", "DE", "DE", 
"DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", 
"DE", "DE", "DE", "DE", "ES", "ES", "ES", "ES", "ES", "ES", "ES", 
"ES", "ES", "ES", "ES", "ES", "ES", "ES", "ES", "ES", "FR", "FR", 
"FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", 
"FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "IT", "IT", "IT", 
"IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", 
"IT", "IT", "IT", "IT", "IT", "IT", "IT"), Date = c(2002, 2003, 
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 
2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2002, 2003, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2018, 2019, 2020, 
2021, 2022, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 
2022, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022
), N = c(10L, 9L, 13L, 14L, 12L, 11L, 16L, 10L, 10L, 13L, 13L, 
28L, 12L, 13L, 16L, 27L, 19L, 14L, 36L, 29L, 7L, 11L, 14L, 6L, 
12L, 16L, 10L, 12L, 14L, 10L, 17L, 4L, 12L, 31L, 26L, 14L, 4L, 
3L, 6L, 44L, 41L, 32L, 49L, 59L, 50L, 64L, 58L, 26L, 36L, 37L, 
32L, 38L, 35L, 36L, 37L, 35L, 34L, 12L, 11L, 3L, 10L, 5L, 11L, 
18L, 17L, 20L, 22L, 30L, 31L, 33L, 28L, 30L, 28L, 24L, 18L, 15L, 
23L, 21L, 6L)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -79L), groups = structure(list(Country = c("DE", 
"DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", 
"DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "ES", "ES", 
"ES", "ES", "ES", "ES", "ES", "ES", "ES", "ES", "ES", "ES", "ES", 
"ES", "ES", "ES", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", 
"FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", 
"FR", "FR", "IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", 
"IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", 
"IT"), Date = c(2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 
2021, 2022, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2011, 2012, 2018, 2019, 2020, 2021, 2022, 2002, 2003, 2004, 2005, 
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 
2017, 2018, 2019, 2020, 2021, 2022, 2002, 2003, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 
2018, 2019, 2020, 2021, 2022), .rows = structure(list(1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
    16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 
    28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 
    40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 
    52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 
    64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 
    76L, 77L, 78L, 79L), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -79L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

I would like to get the following results (where n stands for numbers):

Year   IT   DE ES   FR

2002   n   n    n   n  
2003   n   n    n   n
2004   n   n    n   n
2005   n   n    n   n
2006   n   n    n   n
.
.
.
2022  n   n     n   n

I tried to use pivot_longer in the following way (and many other failed attempts):

df %>% pivot_longer(Date, names_to = ".value", values_to = ".value")

Can anyone help me with this?

CodePudding user response:

names_from() needs to be the columns you want and values_from() should be the associated column values:

df %>% pivot_wider(names_from="Country", values_from="N")
# A tibble: 21 × 5
# Groups:   Date [21]
    Date    DE    ES    FR    IT
   <dbl> <int> <int> <int> <int>
 1  2002    10    11     3    11
 2  2003     9    14     6     3
 3  2004    13     6    44    10
 4  2005    14    12    41     5
 5  2006    12    16    32    11
 6  2007    11    10    49    18
 7  2008    16    12    59    17
 8  2009    10    14    50    20
 9  2010    10    10    64    22
10  2011    13    17    58    30
# … with 11 more rows
  • Related