r - tidyr::pivot_wider() 错误!无法对不存在的列进行子集化

标签 r tidyr

我不确定为什么我不断收到此错误,但如果您有任何见解,我们将不胜感激。该错误不断发生在 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. 

提前谢谢您!

最佳答案

我们不需要 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 - 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.


该错误在文档示例中可以重现

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

关于r - tidyr::pivot_wider() 错误!无法对不存在的列进行子集化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75327224/

相关文章:

r - 如何应用操作 data.table 并使用 2 个或更多值作为参数 R 的函数

r - 检查每个具有相同唯一 ID 的唯一值

r - 通过匹配R中的字符串将行转换为列

r - 使用 R 句子中的连续字符串匹配

R 将元素替换为另一个矩阵中具有相应行号的元素

r - R 的 data.table::fread 中的 "Select"参数

regex - 使用 tidyr 从列中提取值

R:将隐式缺失值和组填充到数据的整个时间跨度

r - 在 R 中创建条件新变量

rbundler 构建错误 : "cannot open file ' startup. Rs':没有这样的文件或目录”