[R] Return a list

Bert Gunter gunter.berton at gene.com
Fri Sep 26 22:46:10 CEST 2008

But why do this? Just leave the (preferably named) variables as list
components and work with them there.

1. ?comment tells you how to add a comment attribute to the list for self
documentation (what were the components? how are they related? etc.)

2. ?with  shows you how to access the components of the list individually in
the "usual" way.

3. ?lapply and friends shows you how to "loop" over list components .



Bert Gunter 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Wacek Kusnierczyk
Sent: Friday, September 26, 2008 12:40 PM
To: Mike Prager
Cc: R help
Subject: Re: [R] Return a list

Mike Prager wrote:
> "Stefan Fritsch" <fritsch at bips.uni-bremen.de> wrote:
>> I have several output variables which I give back with the list command.
>> test <- function	{return(list(a,b,c,d,e,f,g,...))}
>> After the usage of the function I want to assign the variables to the
output variables.
>> result <- test()
>> a <- result$a
>> b <- result$b
>> c <- result$c
>> d <- result$d
>> ...
>> is there a more elegant way to assign these variables, without writing
them all down?

arguably ugly and risky, but simple:

for (name in names(result)) assign(name, result[[name]])

(note, for this to work you actually need to name the components of the
returned list: return(list(a=a,b=b,...)))


R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list