How do I convert a logical variable to factor in Rattle

user3497321 picture user3497321 · Apr 16, 2014 · Viewed 9.3k times · Source

I am using Rattle to run randomForest against my training data set. One of the variables has values FALSE and TRUE.

     > 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 ...

I am able to convert the same to a factor in 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 ...

When I write the data.frame to a CSV and load it using Rattle, again I am seeing it as logical only. Due to this, I am getting the error, Error in na.roughfix.data.frame(x) + na.roughfix only works for numeric or factor

Any help is appreciated. Thanks in advance

Answer

I think you should try including "as"

mydata$IsHoliday=as.factor(mydata$IsHoliday)