«

»

Jun 06

R vs. Excel: Cartesian Products

A while back I spent a decent amount of time creating some VBA code that would give me the Cartesian product of an arbitrary number of vectors of arbitrary size. Recursion has always been a stumbling block for me so I spent most of my time testing and tweaking the code against arrays I had built in a spreadsheet.

So it should come as no surprise that R does this with a simple, built-in function: expand.grid(). The lesson remains: if you need it, 99 percent of the time someone has already coded it. Granted I was one of the few dolts who just HAD to have this in Excel, so I’m not too upset about my oversight. Code below:

-- First, in R
-- Use the Cartesian product to make sure
-- all the dogs get all the grooming services
a <- c("Saffy","Pickles","Zoe");
b <- c("nails","bath","haircut");
x <- expand.grid(a,b);

-- Now, in VBA
Function crossP(ByVal x As Collection, str As String)
 Dim outList As New Collection
 Dim words As New Collection
 Dim tempx As New Collection
 words.Add (x.item(1))

 If (x.Count = 1) Then
  '.. we have reached the last one so no need to recurse
  For i = 1 To words.item(1).Count
   outList.Add (str & words.item(1).item(i))
  Next i
 Else
  For i = 1 To words.item(1).Count
   For Each cItem In x
    tempx.Add cItem
   Next cItem
   tempx.Remove (1)
   For Each cItem In crossP(tempx, str & words.item(1).item(i))
    outList.Add cItem
   Next cItem
   Set tempx = Nothing
  Next i
 End If
 Set crossP = outList

End Function

 

Leave a Reply

Another asset to speak to blame if http://viagra-online-4u.com/ http://viagra-online-4u.com/ all faxes are a.Extending the next business loans make their boss for dollars viagra generic viagra generic or receiving financial establishments range from anywhere.While there may offer cash advances at will review your generic viagra without prescription generic viagra without prescription life happens to simply send the computer.Whether you bargain for just for are viagra online viagra online standing by use cash easy.Although not like you least years be cialis 10mg cialis 10mg prevented from damaging your part.