需要在循环的每次迭代中存储从函数调用返回的结果。
for (i in 1:x)
{
datax <- trainingdata[[i]]
levels(datax$label) <- c(-1, 1)
train_x<-datax[, -1]
train_x<-data.matrix(train_x)
trainy<-datax[, 1]
s_x<-svm.fs(train_x, y=trainy, fs.method="scad",
cross.outer=0, grid.search="discrete",
lambda1.set=lambda1.scad, parms.coding="none",
show="none", maxIter=1000, inner.val.method="cv",
cross.inner=5, seed=seed, verbose=FALSE)
# Store the result s_x
}
s_x需要与名称一起存储(名称已存在于矢量“名称”中)。
我尝试了以下内容:
s_x_results <- data.frame(x=names, y=data.frame(length(trainingdata)))
for (i in 1:x)
{
datax <- trainingdata[[i]]
levels(datax$label) <- c(-1, 1)
train_x<-datax[, -1]
train_x<-data.matrix(train_x)
trainy<-datax[, 1]
s_x<-svm.fs(train_x, y=trainy, fs.method="scad",
cross.outer=0, grid.search="discrete",
lambda1.set=lambda1.scad, parms.coding="none",
show="none", maxIter=1000, inner.val.method="cv",
cross.inner=5, seed=seed, verbose=FALSE)
s_x_results[i]$y <- s_x
}
但我收到错误消息:
Error in `$<-.data.frame`(`*tmp*`, "scad.fix", value = list(classes = c(1L, :
replacement has 11 rows, data has 91
最终对象s_x_results保存在某个文件中以供将来通信。
答案 0 :(得分:3)
尝试replicate
res <- replicate(x,funcall)
答案 1 :(得分:2)
尝试lapply
myFunc <- function(i) {
datax <- trainingdata[[i]]
levels(datax$label) <- c(-1, 1)
train_x<-datax[, -1]
train_x<-data.matrix(train_x)
trainy<-datax[, 1]
svm.fs(train_x, y=trainy, fs.method="scad",
cross.outer=0, grid.search="discrete",
lambda1.set=lambda1.scad, parms.coding="none",
show="none", maxIter=1000, inner.val.method="cv",
cross.inner=5, seed=seed, verbose=FALSE)
}
s_x_results <- lapply(1:x, myFunc)
#Access individual results using s_x_results[[1]], s_x_results[[2]] and so on.
这应该为您提供x个数据帧的列表。
PS:未测试的功能,因为没有提供样本数据。