简体   繁体   中英

Variable number of arguments in r decision tree

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM