简体   繁体   English

在R中返回对象或多个变量(非数字)

[英]Returning objects or multiple variables (not numbers) in R

I am training multiple SVMs in my R application, and I would like my function to return a holder object to which I can easily access each one of them later in a function. 我正在R应用程序中训练多个SVM,我希望函数返回一个holder对象,以后可以在函数中轻松访问每个对象。

The SVM is created as this: SVM创建如下:

svm.model <- svm(x=trainset,y=trainlabels)
svm.pred <- predict(svm.model,testset)

res <- table(pred = svm.pred, true = testlabels)  

I want to be able to return the table called "res", or both svm.model and svm.pred. 我希望能够返回名为“ res”的表,或者返回svm.model和svm.pred。 Any will do. 任何都会做。

In a Object Oriented language I would use something like Javas ArrayList to do this, but a regular list(which was suggested in many posts) didn't do its job. 在面向对象的语言中,我将使用Javas ArrayList之类的方法来执行此操作,但是常规列表(许多帖子中都建议使用)无法完成其工作。 I was not able to put any of these values, and if I made it, they got "unwrapped" and lost its structure. 我无法提供任何这些值,如果我做到了,它们将被“解开”并失去其结构。

Update Good point in the comment section about how exactly I tried to do this with a list. 在评论部分中更新有关我如何尝试使用列表的准确点。 I did the following: 我做了以下工作:

result<-list()

for i in...
   result[i]<-svm.model
   result[i+1] <-svm.pred

I have tried the same as above, but with "res"(a table). 我已经尝试了与上面相同的方法,但是使用了“ res”(一个表)。

Another approach I tried was to create a new list with the previous list each time: result<-list(result,svm.model,svm.pred) 我尝试的另一种方法是每次都使用先前的列表创建一个新列表:result <-list(result,svm.model,svm.pred)

It gives me only a length 3 size list(I see why) and the two last values are "lists" instead of what they are in fact, hence it doesn't allow me to call "table(pred = svm.pred, true = testlabels) " as it returns an error: 它只给我一个长度为3的大小列表(我明白为什么),最后两个值是“列表”,而不是它们实际上的大小,因此不允许我调用“ table(pred = svm.pred,true = testlabels)“,因为它返回错误:

"Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?" “ sort.list(y)中的错误:'x'对于'sort.list'必须是原子的。您在列表上调用过'sort'吗?”

If you have a loop and you want to store all three 如果您有一个循环并且要存储所有三个

result<-list()
for (i in 1:n) {
    ...
    svm.model <- svm(x=trainset,y=trainlabels)
    svm.pred <- predict(svm.model,testset)

    res <- table(pred = svm.pred, true = testlabels)  
    result[[i]] <- list(res=res, svm.model=svm.model, svm.pred=svm.pred)
}

Note is is important to use [[ ]] when putting items into a list and getting them out. 在将项目放入列表并将其取出时,使用[[ ]]时,注意很重要。 I also used a named list here to make it easier to get specific objects out. 我在这里还使用了一个命名列表,以便更轻松地取出特定对象。 Then if you wanted to re-make the table from the 3rd loop, you could do 然后,如果您想从第3个循环中重新制作表格,则可以

table(pred = result[[3]]$svm.pred, true=testlabels)

Thank you for the answer! 谢谢您的回答! I managed to overcome it also by doing the following: 我还通过执行以下操作来克服它:

result[folders[i]]<-list(res)

Where folders is a vector of names. 其中文件夹是名称的向量。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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