r - 错误:无法对不存在的列进行子集化 - 从 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
解决方案
我们可以在转换为后使用反引号来选择tibble
forData %>%
as_tibble %>%
select(Month, `.mean`)
推荐阅读
- python - 使用 python 将 10/11/2018 日期字符串格式转换为 excel 日期值格式
- php - selected='selected' 来自 mysql 的 Multiple Dropdown 中的值
- python - 如何像使用 pandas 一样使用 Pyspark DataFrame 创建 DataFrame?
- java - 如何使用 ActionListener 检测是否选择了单个 JRadioButton?
- android - 关闭并再次打开应用程序时,带有 Viewpager 的片段不刷新
- javascript - 在更新时获取 Codemirror 文本框值
- html - 允许调整大小以忽略最大高度?
- javascript - Angular 2从数组中添加或删除项目
- php - 类 Illuminate\Database\Eloquent\Builder 的对象外键问题
- php - 如何最小化sql查询中的代码重复?