Is "wrap in a list" the best way to return a number of objects for a
function?
For example, my current approach is:
function(){
ret <- list()
ret$a <- ...
ret$b <- ...
...
ret$e <- ...
return(ret)
}
Then I realise that this could result in O(n^2) performance, since R
always copy by values here.
So that I came up with this:
function(){
a <- ...
b <- ...
...
e <- ...
ret <- list(a=a,b=b,...,e=e) # ***
return(ret)
}
On the line *** I have to do like a=a to expose the name of the entry. If
the number of the objects I would like to add is large, this could be
tedious.
What is the proper way of achieving my goals?
Update: in the 2nd way every object is only duplicated once, but I am
worried that in the 1st way the whole list could be copied each time I add
objects to it.
No comments:
Post a Comment