La fonction pivot_longer ne fonctionne pas

Bonjour Chers tous,
J’ai probleme avec la fonction pivot_longer,
voici mon code :

pacman::p_load(
  here,
  rio,
  lubridate,
  janitor,
  ggplot2,
  flextable,
  tidyverse
)

database_wb <- import(here("data","countries.xlsx"))
database_wb %>% pivot_longer(
  cols = 2019:2022,
  names_to="year",
  values_to="values_nbr"
)

J’ai un message d’erreur :

Error in loc_validate():
! Can’t subset columns past the end.
:information_source: Locations 2019, 2020, 2021, and 2022 don’t exist.
:information_source: There are only 5 columns.
Mon DataFrame :
Capture d’écran 2022-12-29 à 17.34.51

The column names must be strings
I think this would work

 pivot_longer(
  cols = "2019":"2022",
  names_to="year",
  values_to="values_nbr"
)

as suggested here: RPubs - Week 8_Assignment

1 Like

Hi @a_najeeb
That’s run
many thanks