6

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

user3497321
  • 443
  • 2
  • 6
  • 15
  • 3
    Sounds like automatic type conversion - maybe try `mydata$IsHoliday <- factor(ifelse(mydata$IsHoliday,"yes","no"))` to force entries to look nothing like a logical - or if you have access to the function that's reading in the data, look at the `colClasses` setting to force the csv to be read in as you wish: `read.csv(..., colClasses=c("integer","character")...` – Gavin Kelly Apr 16 '14 at 10:37
  • I dont have the access to the function that reads CSV in `rattle`. I am just browsing the file and executing. – user3497321 Apr 16 '14 at 10:52
  • 1
    OK, so you'll probably have to try the `ifelse` solution - does that work? – Gavin Kelly Apr 16 '14 at 12:41
  • Re comment about "access to the function".. have a look at the log tab in Rattle for the actual code that is being executed. You can take that code and modify to suit the read.csv as suggested here to load the data into R, then in the Data tab choose the RDataSet option to access this dataset within Rattle, and build the forest. – Graham Williams Apr 24 '18 at 08:09

1 Answers1

5

I think you should try including "as"

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