Skip to content
Prev 313997 / 398506 Next

Filling Lists or Arrays of variable dimensions

Jessica

This is super ugly but assigns as you requested in the original post:



height<-c("high","low")  

width<-c("slim","wide")  

doSomething <- function() {   
    cat("done...\n\n")   
    return(list(a = list(1), b = list(2), c = list(3))) }    


tempList <- list()  

for(h in height){  
    tempList[[h]] <- list()  
    for(w in width){  
    tempList[[h]][[w]] <- NA   
    }  
}  
  
gridDF <- expand.grid(height, width, stringsAsFactors = FALSE)  
  
assignToGridValueInList <- function(x) { tempList[[as.character(x["Var1"])]][[as.character(x["Var2"])]] <<- doSomething() }  
  
lapply(split(gridDF, gridDF), assignToGridValueInList)  



It is probably also possible to create the output object dynamically, but it is not a good idea. Assigning a new element to an object creates a copy of that object, so for very complicated structures you will run into speed and memory issues. Assigning to an existing element is clean, and will normally be less wasteful than creating a new object each time.

Best wishes

Chris


Chris Campbell
Tel. +44 (0) 1249 705 450?| Mobile. +44 (0) 7929 628349
mailto:ccampbell at mango-solutions.com?| http://www.mango-solutions.com
Mango Solutions
2 Methuen Park
Chippenham
Wiltshire 
SN14 OGB
UK

-----Original Message-----
From: Jessica Streicher [mailto:j.streicher at micromata.de] 
Sent: 20 December 2012 18:01
To: William Dunlap
Cc: Chris Campbell; R help
Subject: Re: [R] Filling Lists or Arrays of variable dimensions

Really must have been unclear at some point, sorry.

William, thats interesting, but not really helping the main problem, which is: how to do
without having initialized the list in the loop before. 

Well, or how to initialize it without having to do the loop thing, because the loop stuff can only be done for a specific set of parameter vectors. But those change, and i don't want to have to write another loop construct every time for the new version.

I want to say: hey, i have these vectors here with these values (my parameters), could you build me that nested list structure (tree - whatever) from it? And the function will give me that structure whatever i give it without me needing to intervene in form of changing the code.

-------------- Clarification -----------------

First: i am not computing statistics over the parameters. I'm computing stuff from other data, and the computation is affected by the parameters. 

I am computing classifiers for different sets of parameters for those classifiers. So the result of doSomething() isn't a simple value. Its usually a list of 6 lists (doing cross validation), which in turn have the classifier object, some statistics of the classifier (e.g what was missclassified), and the subsets of data used in them.
That doesn't really fit in a data.frame, hence the use of lists. I want the nested lists because it helps me find stuff in the object browser faster, and because all my other code is already geared towards it. If i had the time i might still go for a flat structure that everyone keeps telling me to use (got a few mails off the list), but i really haven't the time.

If theres no good way i'll just keep things as they are now.
On 20.12.2012, at 18:37, William Dunlap wrote:

            
--

LEGAL NOTICE\ \ This message is intended for the use of ...{{dropped:18}}