我不知道为什么我总是得到这个错误,但如果你有任何见解,这将是非常感谢。错误总是发生在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.
先谢谢你!
1条答案
按热度按时间ddrv8njm1#
我们不需要
id_cols
中的Group
根据
?pivot_wider
id_cols -唯一标识每个观测的一组列。默认为数据中的所有列,但names_from和values_from**中指定的列除外。通常在具有冗余变量(即其值与现有变量完全相关的变量)时使用。
该错误在文档示例中可重现