I was just getting started with some practice with decision trees and I encountered this error regarding varying lengths between columns but after checking each column length I saw that they were all the same size. I compiled this code in RStudio.
library(tree)
library(tidyverse)
df <- subset(read.csv("Steel_industry_data.csv"), select = c(2, 3, 4, 5, 6, 7, 8))
df <- rename(df, CO2_Usage = "CO2.tCO2.",
LeadingPowerFactor = "Leading_Current_Power_Factor",
LaggingPowerFactor = "Lagging_Current_Power_Factor",
LeadingReactivePower = "Leading_Current_Reactive_Power_kVarh",
LaggingReactivePower = "Lagging_Current_Reactive.Power_kVarh")
set.seed(1234)
i <- sample(1:nrow(df), nrow(df)*0.75, replace = FALSE)
train <- df[i,]
test <- df[-i,]
#Here the error generates, I checked the length of columns using length(df$*Variable*)
tree1 <- tree(df$CO2_Usage~., data=train)
Link to the dataset I used can be found here. https://archive.ics.uci.edu/ml/datasets/Steel+Industry+Energy+Consumption+Dataset