Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a logical variable to factor in Rattle

Tags:

r

rattle

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

like image 375
user3497321 Avatar asked Apr 16 '14 10:04

user3497321


People also ask

How do you turn a variable into a factor variable?

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.

How do you convert a continuous variable to a categorical in R?

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.

How do you convert numerical data to categorical data in R?

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


1 Answers

I think you should try including "as"

mydata$IsHoliday=as.factor(mydata$IsHoliday)   
like image 131
Santiago Roberto Déniz Santana Avatar answered Oct 13 '22 09:10

Santiago Roberto Déniz Santana