我想这样做:
library(party)
# cts <- ???
n <- c(50, 100, 116)
for(i in 1:length(n)) {
data <- airq[1:n[i], ]
airct <- ctree(Ozone ~ ., data)
cts[i] <- airct
}
..但不知道我可以使用哪种类型的对象来分配ctree结果。
感谢您的任何指示, 凯
答案 0 :(得分:3)
list
通常就是答案。
library(party)
airq <- na.omit(airquality) # Prunes NA rows down to 111 rows...
n <- c(50, 100, 111) # 116 is outside
cts <- vector('list', length(n))
for(i in 1:length(n)) {
data <- airq[1:n[i], ]
airct <- ctree(Ozone ~ ., data)
cts[[i]] <- airct
}
但更好的方法是在这里使用lapply
(list-apply)。不需要for-loop并返回一个列表。
library(party)
airq <- na.omit(airquality) # Prunes NA rows down to 111 rows...
n <- c(50, 100, 111) # 116 is outside
cts <- lapply(n, function(ni) ctree(Ozone ~ ., data=airq[1:ni,]))