GitHub_Repo
GitHub_Repo

Reputation: 11

Why is my levels function returning null for all of my variables?

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.

Upvotes: 0

Views: 4335

Answers (1)

GitHub_Repo
GitHub_Repo

Reputation: 11

tamtam's comment worked. I just added dataset$Species <- as.factor(dataset$Species) to my code after colnames(dataset) <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")

Upvotes: 1

Related Questions