Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

tidyr::pivot_wider() error ! Can't subset columns that don't exist

I’m not sure why I keep getting this error but if you have any insight it would be greatly appreciated. The error keeps happening at the tidyr::pivot_wider section.
Below is the full code:

library(tidyr)
library(dplyr)
library(stringr)

impacts = impact %>%
    tidyr::pivot_longer(PI_Direct_Impact:E_Total,
                        names_to = "names",
                        values_to = "values") %>%
    dplyr::mutate(
      Impact_Type = dplyr::case_when(
        stringr::str_detect(names, "PI_") ~ "Income Impacts",
        stringr::str_detect(names, "TV_") ~ "Total Value Added",
        stringr::str_detect(names, "O_") ~ "Output Impacts",
        stringr::str_detect(names, "E_") ~ "Employment Impacts"
      )
    ) %>%
    dplyr::mutate(
      Group = dplyr::case_when(
        stringr::str_detect(names, "Direct_Impact") ~ "Direct",
        stringr::str_detect(names, "Indirect_Impact") ~ "Indirect",
        stringr::str_detect(names, "Induced_Impact") ~ "Induced",
        stringr::str_detect(names, "Total") ~ "Total"
      )
    ) %>%
    dplyr::select(-names) %>%
    tidyr::pivot_wider(
      id_cols = c(
        fips,
        `Economic Category`,
        `Species Category`,
        spec_no,
        Impact_Type,
        Group,
        Imports
      ),
      names_from = Group,
      values_from = values
    )

I cannot share the data but here are the column headers right before the pivot_wider function:

# A tibble: 6 x 8
   fips `Economic Category` Species~1 spec_no Imports values Impac~2 Group
  <dbl> <chr>               <chr>       <dbl> <chr>    <dbl> <chr>   <chr>

This is the full error that I keep getting:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error in `select_wider_id_cols()`:
! Can't subset columns that don't exist.
x Column `Group` doesn't exist. 

Thank you in advance!

>Solution :

We don’t need the Group in id_cols

...
%>%
tidyr::pivot_wider(
      id_cols = c(
        fips,
        `Economic Category`,
        `Species Category`,
        spec_no,
        Impact_Type,
        
        Imports
      ),
      names_from = Group,
      values_from = values
    )

According to ?pivot_wider

id_cols – A set of columns that uniquely identifies each observation. Defaults to all columns in data except for the columns specified in names_from and values_from. Typically used when you have redundant variables, i.e. variables whose values are perfectly correlated with existing variables.


The error is reproducible in the documentation example

# without specifying id_cols
> fish_encounters %>%
  pivot_wider(names_from = station, values_from = seen) %>%
  head
# A tibble: 6 × 12
  fish  Release I80_1 Lisbon  Rstr Base_TD   BCE   BCW  BCE2  BCW2   MAE   MAW
  <fct>   <int> <int>  <int> <int>   <int> <int> <int> <int> <int> <int> <int>
1 4842        1     1      1     1       1     1     1     1     1     1     1
2 4843        1     1      1     1       1     1     1     1     1     1     1
3 4844        1     1      1     1       1     1     1     1     1     1     1
4 4845        1     1      1     1       1    NA    NA    NA    NA    NA    NA
5 4847        1     1      1    NA      NA    NA    NA    NA    NA    NA    NA
6 4848        1     1      1     1      NA    NA    NA    NA    NA    NA    NA
# with specifying id_cols and names_from the same column
> fish_encounters %>%
   pivot_wider(id_cols = c(fish, station), names_from = station, values_from = seen)
Error in `select_wider_id_cols()`:
! Can't select columns that don't exist.
✖ Column `station` doesn't exist.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading