Home > Software engineering >  tidyr::pivot_wider() error ! Can't subset columns that don't exist
tidyr::pivot_wider() error ! Can't subset columns that don't exist

Time:02-03

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:

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

Thank you in advance!

CodePudding user response:

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.
  • Related