tidyr::pivot_wideer()错误!无法为不存在的列设置子集

ljo96ir5  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(346)

我不知道为什么我总是得到这个错误,但如果你有任何见解,这将是非常感谢。错误总是发生在tidyr::pivot_wider部分。以下是完整的代码:

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
    )

我不能共享数据,但下面是pivot_wider函数前面的列标题:

# 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>

这是我不断得到的完整错误:

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

先谢谢你!

ddrv8njm

ddrv8njm1#

我们不需要id_cols中的Group

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

根据?pivot_wider
id_cols -唯一标识每个观测的一组列。默认为数据中的所有列,但names_from和values_from**中指定的列除外。通常在具有冗余变量(即其值与现有变量完全相关的变量)时使用。
该错误在文档示例中可重现

# 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.

相关问题