Im writing a machine learning code for my dataset having hotels column.The hotel column contains 300 hotels name.For data preprocessing,I saw we have to use factor.Is there any easy way to covert it as there are so many values for level?
Asked
Active
Viewed 31 times
2 Answers
1
It's simple, use the as.factor()
function to convert the column form character
to factor
.
Here's a sample
# Sample data
data
a b
1 A 1
2 B 2
3 C 3
4 A 4
5 B 5
class(data$a)
[1] "character"
# Converting to factor
data$a <- as.factor(data$a)
# Results
class(data$a)
[1] "factor"
summary(data$a)
A B C
2 2 1

skillsmuggler
- 1,862
- 1
- 11
- 16
1
if you are using read.csv option to load the csv data into a dataframe, then column having string values are by default loaded as a factor column.
Anyway you can use factor() function to convert a column to factor: df$a <- factor(df$a).

Soumen Das
- 31
- 1
- 7