我想在應用機器學習方法之前標準化我的所有變數。但是,據我了解,虛擬變數永遠不應該標準化。輸入以下代碼后,r 標準化了我所有的變數,甚至是二進制變數。我怎樣才能避免這種情況發生?
#standardize all non-categorical variables to have mean zero and a standard deviation of one
df_standardized <- df %>% mutate(across(where(is.numeric), scale))
我檢查了我的資料型別是“int”,而不是數字。預先感謝您的幫助。
uj5u.com熱心網友回復:
scaleas.numeric回傳一個矩陣,我們可以通過或將矩陣轉換為向量as.vector。另外,inherits僅用于修改numeric列
library(dplyr)
out <- df %>%
mutate(across(where(~ inherits(.x, "numeric")),
~ as.numeric(scale(.x))))
資料
data(iris)
df <- iris
df$intCol <- 1L
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/529899.html
下一篇:基于R中的最大條件回填行
