我有几个名为forest.1、forest.2、forest.3等的RandomForest公式。想使用“for”迭代来一一阅读它们,例如:
for(i in 1:20){
model = forest.i
predict.y = predict(model, test.x)
}
当然,forest.i(i从1到20)不能被识别为20个公式。我该怎么做才能让它发挥作用?谢谢!
最佳答案
您可以使用函数get
,例如model <- get(sprintf("forest.%i", i))
。这将创建一个字符串,例如 forest.1
并尝试获取具有该名称的对象。
关于r - 如何识别随机森林公式名称中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40228739/