Reading rpart Input Parameters from a Text Variable

前端 未结 2 1025
自闭症患者
自闭症患者 2021-01-20 14:03

I\'m using rpart to make a decision tree. For example:

fit <- rpart(Kyphosis ~ Age + Number + Start, data=kyphosis)

How do

相关标签:
2条回答
  • 2021-01-20 14:27

    Try simply passing the formula as a character string:

    rpart_formula <-paste("Kyphosis ~ ",paste(predictor_variables, collapse="+"))
    

    that should be coerced to a formula by rpart.

    Edit

    As noted in the comments below, not all functions will do the coercion for you, so you should not rely on this behavior, but in this case rpart most certainly does.

    0 讨论(0)
  • 2021-01-20 14:35

    Use as.formula:

    rpart_formula <- as.formula(
        paste("Kyphosis ~ ", 
              paste(predictor_variables, collapse = " + "), 
              sep = ""
        )
    )
    
    0 讨论(0)
提交回复
热议问题