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
In R, you can convert multiple numeric variables to factor using lapply function. The lapply function is a part of apply family of functions. They perform multiple iterations (loops) in R. In R, categorical variables need to be set as factor variables.
You can use the cut() function in R to create a categorical variable from a continuous one. Note that breaks specifies the values to split the continuous variable on and labels specifies the label to give to the values of the new categorical variable.
Firstly, we will convert numerical data to categorical data using cut() function. Secondly, we will categorize numeric values with discretize() function available in arules package (Hahsler et al., 2021).
I think you should try including "as"
mydata$IsHoliday=as.factor(mydata$IsHoliday)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With