[R] Question on lists and vectors of lists

Ajay Shah ajayshah at mayin.org
Thu Jun 17 19:26:29 CEST 2004


I have an elementary programming question. Could someone please point
me in the right direction?

I have a function which will run for thousands of companies. At each
invocation, it returns 2 numbers. I plan to do something like:

  think_one_firm <- function(filename) {
     # Do stuff
     return(list(x=x,y=y))
  }

So for each of the firms in my dataset, I will call
think_one_firm(datafilename) and it will give me back two numbers x
and y. I could say:

  l = think_one_firm("blah")
  print(l$x); print(l$y);

and all would be fine.

What I want to do is: To tuck away the returned lists into a vector or
a data frame.

At the end, I would like to endup with a data structure allfirms
containing one 'row' for each firm. I guess this could be a data
frame, or a matrix, or a vector of lists (if such a thing exists). In
case it's a data frame, I would say allfirms$x[400] to access the 'x'
value returned for the 400th firm.

How would I do this?

-- 
Ajay Shah                                                   Consultant
ajayshah at mayin.org                      Department of Economic Affairs
http://www.mayin.org/ajayshah           Ministry of Finance, New Delhi




More information about the R-help mailing list