> diamonds_train_processed <- prep(diamonds_recipe) %>% juice(all_of(names(diamonds_train))) Error in `all_of()`: ! Can't subset columns that don't exist. ✖ Columns `cut`, `color`, and `clarity` don't exist. Backtrace: 1. prep(diamonds_recipe) %>% juice(all_of(names(diamonds_train))) 23. tidyselect::all_of(names(diamonds_train))
时间: 2024-03-03 17:46:48 浏览: 378
deck-of-cards.zip_card of deck_deck of card_deck-of_deck-of-c
这个错误的原因是`all_of()`函数不能处理数据集中的非字符列(non-character columns),而`diamonds_train`数据集中包含非字符列。要解决这个问题,你需要使用`select()`函数选择所有需要的列,并将其传递给`juice()`函数。修改后的代码如下:
```
diamonds_train_processed <- prep(diamonds_recipe) %>%
bake(new_data = NULL) %>%
select(contains("price"), contains("cut"), contains("color"), contains("clarity")) %>%
juice()
```
这将使用`bake()`函数将处理后的数据集应用于`diamonds_train`数据集,并使用`select()`函数选择所有需要的列。然后,`juice()`函数将返回处理后的数据集。请注意,我使用了`contains()`函数来选择所有包含指定字符串的列。这将确保即使列的顺序发生变化,代码也不会出错。
阅读全文