> 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 19:46:48 浏览: 384
这个错误的原因是`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()`函数来选择所有包含指定字符串的列。这将确保即使列的顺序发生变化,代码也不会出错。
相关问题
> diamonds_train_processed <- prep(diamonds_recipe) %>% juice(diamonds_train) Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0. ℹ Please use `all_of()` or `any_of()` instead. # Was: data %>% select(diamonds_train) # Now: data %>% select(all_of(diamonds_train)) See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>. This warning is displayed once every 8 hours. Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated. Error in `juice()`: ! Can't subset columns with `diamonds_train`. ✖ `diamonds_train` must be numeric or character, not a <tbl_df/tbl/data.frame> object. Backtrace: 1. prep(diamonds_recipe) %>% juice(diamonds_train) 17. rlang::cnd_signal(x)
这个错误的原因是因为`diamonds_train`是一个数据框(data frame),而不是字符向量(character vector)或数值向量(numeric vector)。因此,你需要传递`diamonds_train`中所有要保留的列的名称。修改后的代码如下:
```
diamonds_train_processed <- prep(diamonds_recipe) %>% juice(all_of(names(diamonds_train)))
```
这将使用`all_of()`函数将`names(diamonds_train)`转换为字符串向量,以便`juice()`函数可以正确选择处理后的数据集中的列。
> library(rsample) > library(tidymodels) > library(recipes) > > # Create preprocessing recipe > diamonds_recipe <- recipe(price ~ ., data = diamonds_train) %>% + step_boxcox(all_numeric()) %>% + step_dummy(all_nominal(), -all_outcomes()) Error in step_boxcox(., all_numeric()) : could not find function "step_boxcox"
这个错误的原因是因为`step_boxcox()`函数在`recipes`包中已经被移除了。现在,你需要使用`step_YeoJohnson()`函数来替换它。修改后的代码如下:
```
library(rsample)
library(tidymodels)
library(recipes)
# Create preprocessing recipe
diamonds_recipe <- recipe(price ~ ., data = diamonds_train) %>%
step_YeoJohnson(all_numeric()) %>%
step_dummy(all_nominal(), -all_outcomes())
```
这将使用Yeo-Johnson变换来预处理数值变量,而不是Box-Cox变换。请注意,这可能会对你的模型产生不同的影响,因为两种变换的效果会有所不同。
阅读全文