如何将逻辑变量转换为 Rattle 中的因子

use*_*321 5 r rattle

我正在使用 Rattle 来运行randomForest我的训练数据集。其中一个变量具有值FALSETRUE

     > str(mydata)
     'data.frame':  421570 obs. of  2 variables:
     $ Trial       : int  1 1 1 1 1 1 1 1 1 1 ...
     $ IsHoliday   : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
Run Code Online (Sandbox Code Playgroud)

我能够将其转换为 R 中的一个因子。

     > mydata$IsHoliday <- factor(mydata$IsHoliday)
     > str(mydata)
     'data.frame':  421570 obs. of  2 variables:
     $ Trial       : int  1 1 1 1 1 1 1 1 1 1 ...
     $ IsHoliday   : Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...
Run Code Online (Sandbox Code Playgroud)

当我将 写入data.frameCSV 并使用 加载它时Rattle,我再次将其视为合乎逻辑的。因此,我收到错误消息,Error in na.roughfix.data.frame(x) + na.roughfix only works for numeric or factor

任何帮助表示赞赏。提前致谢

小智 5

我认为你应该尝试包括“as”

mydata$IsHoliday=as.factor(mydata$IsHoliday)   
Run Code Online (Sandbox Code Playgroud)