Re: [R] array of lists? is this the best way to do it?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed 07 Dec 2005 - 01:05:22 EST

On 12/6/05, John McHenry <john_d_mchenry@yahoo.com> wrote:
> [Q.] How to create an array of lists, or structures the most elegant way?
>
> There have been questions in the past but none too recently...I want to know if the following looks OK to you guys or if there is a better way to create an array of lists:
>
> # PREAMBLE ... JUST TO GET THINGS GOING
> makeList<- function(data, anythingElse) {
> rval <- list( data = data,
> anythingElse = anythingElse
> )
> class(rval) <- "myListOfArbitraryThings"
> return(rval)
> }
> # make up some arbitrary data
> payload<- list( as.matrix(cbind(1,1:3)),
> 10:15,
> data.frame(cbind(x=1, y=1:10), fac=sample(LETTERS[1:3], 10, repl=TRUE))
> )
>
> # HERE'S THE ARRAY-CONSTRUCTION PART THAT I WANT CRITIQUED:
> n<- 3 # number of lists in the array of lists
> v<- vector("list", n) # <--- IS THIS THE BEST WAY TO CREATE AN ARRAY OF LISTS?
> # fill the array with essentially arbitrary stuff:
> for (i in 1:n) v[[i]]<- makeList(payload[[i]], i)

You could use lapply to avoid having to set up the empty list:

lapply(1:n, function(i) makeList(payload[[i]], i)) # untested



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Dec 07 01:26:14 2005

This archive was generated by hypermail 2.1.8 : Wed 07 Dec 2005 - 02:29:16 EST