[R] offlist Re: How can I avoid nested 'for' loops or quicken the process?

David Winsemius dwinsemius at comcast.net
Mon Dec 22 22:32:01 CET 2008


I do agree with Dr Berry that your question failed on several grounds  
in adherence to the Posting Guide, so this is off list.

Maybe this will give you  guidance that you can apply to your next  
question to the list:

 > alist <- list("a","b","c")
 > blist <- list("ab","ac","ad")

 > expand.grid(alist, blist)
   Var1 Var2
1    a   ab
2    b   ab
3    c   ab
4    a   ac
5    b   ac
6    c   ac
7    a   ad
8    b   ad
9    c   ad

 > apply( expand.grid(alist, blist), 1, function(x) paste(x[1], x[2],  
sep=""))
[1] "aab" "bab" "cab" "aac" "bac" "cac" "aad" "bad" "cad"

 > clist <- list("AA","BB")

 > apply(expand.grid(alist, blist, clist),1,function(x) paste(x[1],  
x[2], x[3], sep=""))
  [1] "aabAA" "babAA" "cabAA" "aacAA" "bacAA" "cacAA" "aadAA" "badAA"  
"cadAA" "aabBB"
[11] "babBB" "cabBB" "aacBB" "bacBB" "cacBB" "aadBB" "badBB" "cadBB"

 > dlist <- list(TRUE,FALSE)

 > apply(expand.grid(alist, blist, clist, dlist),1,function(x)  
paste(x[1], x[2], x[3], (x[4]), sep=""))[8:12]
[1] "badAATRUE" "cadAATRUE" "aabBBTRUE" "babBBTRUE" "cabBBTRUE"


This could get unwieldily if the length of the lists are appreciable,  
since the number of rows will be the product of all the lengths. On  
the other hand you could create a dataframe indexed by the variables  
in expand.grid's output:

 >  master.df <- data.frame( expand.grid(alist, blist, clist, dlist),
                             results = apply(expand.grid(alist, blist,  
clist,dlist),1,
                                     function(x) paste(x[1], x[2],  
x[3], (x[4]), sep="")))



-- 
David Winsemius

On Dec 22, 2008, at 3:33 PM, Charles C. Berry wrote:

> On Mon, 22 Dec 2008, Brigid Mooney wrote:
>
>> Hi All,
>>
>> I'm still pretty new to using R - and I was hoping I might be able  
>> to get
>> some advice as to how to use 'apply' or a similar function instead  
>> of using
>> nested for loops.
>
> Unfortunately, you have given nothing that is reproducible.
>
> The details of MyFunction and the exact structure of the list  
> objects are crucial.
>
> Check out the _Posting Guide_ for hints on how to formulate a  
> question that will elecit an answer that helps you.
>
> HTH,
>
> Chuck
>
>
>>
>> Right now I have a script which uses nested for loops similar to  
>> this:
>>
>> i <- 1
>> for(a in Alpha) { for (b in Beta) { for (c in Gamma) { for (d in  
>> Delta) {
>> for (e in Epsilon)
>> {
>>      Output[i] <- MyFunction(X, Y, a, b, c, d, e)
>>       i <- i+1
>> }}}}}
>>
>>
>> Where Output[i] is a data frame, X and Y are data frames, and  
>> Alpha, Beta,
>> Gamma, Delta, and Epsilon are all lists, some of which are numeric,  
>> some
>> logical (TRUE/FALSE).
>>
>> Any advice on how to implement some sort of solution that might be  
>> quicker
>> than these nested 'for' loops would be greatly appreciated.
>>
>> Thanks!
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> Charles C. Berry                            (858) 534-2098
>                                            Dept of Family/Preventive  
> Medicine
> E mailto:cberry at tajo.ucsd.edu	            UC San Diego
> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego  
> 92093-0901
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> 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