Reputation: 643
Thanks for any help - I'm building a decision tree in R, and the classic example is
iris_ctree <- ctree(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data=iris)
My question is: what if I wanted to enter a variable number of parameters, say instead of pre-ordaining Sepal.Length + Sepal.Width and Petal.Length, it was
Flowervar1, Flowervar2, Flowervar3, etc. What if I don't know the number of independent variables until the program is run, how do I pass that into the formula?
Upvotes: 0
Views: 221
Reputation: 643
Based on the excellent recommendations of MrFlick, I found it!
listofintfactors <- c(paste("df", 1:iterations-1, sep = ""))
form <- as.formula(paste("df[,ncol(df)]~", paste (listofintfactors[-1], collapse="+")))
got me the exact formula I needed, and I was then just able to plug that in to the decision tree, and hey presto away it went. "iterations" is the number of variables I'm testing, so this now works for any user-input number.
So: Thanks, MrFlick!
Upvotes: 0