Skip to content

creating list of lists

2 messages · Maas James Dr (MED), Ben Tupper

#
Question 1:
I have a long list of variable names such as

first <- c("one","two","three")

and what I want to do is create a list of lists ... where the names of each of overall lists components are  "one","two", and "three".

This is the same result as 

second <- list(one=list(),two=list(),three=list())

Is there a way to exploit lapply to convert a list such as first to a list of empty lists such as second?

Question 2:

In a parallel routine using foreach and doMPI have to put calcualted values into a list of lists such as 

list(one=one,
     two=two,
     three=three)

It uses the same (large) list of variable names as first in question 1.  Is there a simpler way to accomplish this with a lapply statement?


I've tried several permutations but no luck! Thanks a bunch.

Jim

===============================
Dr. Jim Maas
University of East Anglia
#
Hello,
On Mar 12, 2011, at 8:32 AM, Maas James Dr (MED) wrote:

            
The first question is relatively easy.  Try...

first <- c("one","two","three")
second <- sapply(first, function(x) { vector(mode = "list") })

I am unable to help you with your second question.

Cheers,
Ben
Ben Tupper
Bigelow Laboratory for Ocean Sciences
180 McKown Point Rd. P.O. Box 475
West Boothbay Harbor, Maine   04575-0475
http://www.bigelow.org/