Here is the code that I run beforehand.
library(ggplot2)
library(caret)
filename <- "iris.csv"
dataset <- read.csv(filename, header = FALSE)
colnames(dataset) <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
validation_index <- createDataPartition(dataset$Species, p=0.80sa, list=FALSE)
validation <- dataset[-validation_index,]
dataset <- dataset[validation_index,]
My question is why when I try to run levels(dataset$Species)
all I get is NULL
Species is a character variable and I should get 3 results: Iris-setosa, Iris-versicolor, and Iris-virginica. The code works when I import the dataset directly from R, but not when I try to import a csv file.
is.factor(dataset$Species)
gives you a FALSE, you may turn Species in a factordataset$Species <- as.factor(dataset$Species)
and then trylevels(dataset$Species)
again. Character variables do not have levels. If Species is a character the functionlevels
won't find any levels and results inNULL
. – tamtamdata(iris); levels(iris$Species)
. The issue you're seeing is likely related to whatever is in'iris.csv'
. – andrew_reece