首页 > 解决方案 > 错误:无法对不存在的列进行子集化 - 从 R 中的寓言中选择

问题描述

我正在 R 中构建多个预测,并尝试从预测输出中选择某些列。下面是这个寓言的样子:

> head(forData)
# A fable: 6 x 8 [1M]
# Key:     .model [1]
  .model     Month             ABC .mean DateVar             PCT     Ind1    Ind2
  <chr>      <mth>          <dist> <dbl> <dttm>              <dbl>   <dbl>   <dbl>
1 average 2021 Jul N(0.31, 0.0017) 0.315 2021-07-01 00:00:00  3.25       0       0
2 average 2021 Aug N(0.33, 0.0024) 0.328 2021-08-01 00:00:00  3.25       0       0
3 average 2021 Sep N(0.33, 0.0029) 0.329 2021-09-01 00:00:00  3.25       0       0
4 average 2021 Oct N(0.32, 0.0038) 0.322 2021-10-01 00:00:00  3.25       0       0
5 average 2021 Nov N(0.33, 0.0044) 0.328 2021-11-01 00:00:00  3.25       0       0
6 average 2021 Dec N(0.33, 0.0051) 0.326 2021-12-01 00:00:00  3.25       0       0

当我尝试使用 dplyr 选择任何列时,出现以下错误:

> forData %>% select(Month, .mean)
Error: Can't subset columns that don't exist.
x Column `ABC` doesn't exist.

下面的代码为我提供了 Month 和 .mean 的向量,因此我假设名称是正确的,但我无法理解它给出的错误。

forData$Month
forData$.mean

标签: rdplyrfable-r

解决方案


我们可以在转换为后使用反引号来选择tibble

forData %>% 
        as_tibble %>% 
         select(Month, `.mean`)

推荐阅读