我有一個df像這樣的資料框
height age dept
69 18 A
44 8 B
72 19 B
58 34 C
我只想對因子變數進行單熱編碼(只有 dept 是一個因子)。我怎樣才能做到這一點?
目前我正在選擇一切..
并收到此警告:
警告訊息:下面的變數不是因子的載體,將被忽略:height,age
ohe <- df %>%
recipes::recipe(~ .) %>%
recipes::step_dummy(tidyselect::everything()) %>%
recipes::prep() %>%
recipes::bake(df)
uj5u.com熱心網友回復:
使用wherewithis.factor而不是everything
library(dplyr)
df %>%
recipes::recipe(~ .) %>%
recipes::step_dummy(tidyselect:::where(is.factor)) %>%
recipes::prep() %>%
recipes::bake(df)
-輸出
# A tibble: 4 × 4
height age dept_B dept_C
<int> <int> <dbl> <dbl>
1 69 18 0 0
2 44 8 1 0
3 72 19 1 0
4 58 34 0 1
資料
df <- structure(list(height = c(69L, 44L, 72L, 58L), age = c(18L, 8L,
19L, 34L), dept = structure(c(1L, 2L, 2L, 3L), .Label = c("A",
"B", "C"), class = "factor")), row.names = c(NA, -4L), class = "data.frame")
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/354136.html
上一篇:計算回歸線的輪廓置信區間
下一篇:僅轉置某些列-資料格式
