すべての組み合わせ

```R
size = c("S","M","L")
topping = c("pepperoni","sausage","meatball","extra cheese")
order = c("deliver","pick-up")
```
のような条件があるとき，すべての条件の組み合わせ（上の例だと 3 × 4 × 2 = 24 通りの組み合わせ）をデータフレームなり行列なりで列挙する。

もっとも単純には，以下のように 3 重の for ループを使えばよいのだが...

```R
> pizzas = character(0)
> 
> for(i in 1:length(size)){
+   for(j in 1:length(topping)){
+     for(k in 1:length(order)){
+       pizzas = rbind(pizzas,c(size[i],topping[j],order[k]))
+     }
+   }
+ }
> 
> pizzas
      [,1] [,2]           [,3]     
 [1,] "S"  "pepperoni"    "deliver"
 [2,] "S"  "pepperoni"    "pick-up"
 [3,] "S"  "sausage"      "deliver"
 [4,] "S"  "sausage"      "pick-up"
 [5,] "S"  "meatball"     "deliver"
 [6,] "S"  "meatball"     "pick-up"
 [7,] "S"  "extra cheese" "deliver"
 [8,] "S"  "extra cheese" "pick-up"
 [9,] "M"  "pepperoni"    "deliver"
[10,] "M"  "pepperoni"    "pick-up"
[11,] "M"  "sausage"      "deliver"
[12,] "M"  "sausage"      "pick-up"
[13,] "M"  "meatball"     "deliver"
[14,] "M"  "meatball"     "pick-up"
[15,] "M"  "extra cheese" "deliver"
[16,] "M"  "extra cheese" "pick-up"
[17,] "L"  "pepperoni"    "deliver"
[18,] "L"  "pepperoni"    "pick-up"
[19,] "L"  "sausage"      "deliver"
[20,] "L"  "sausage"      "pick-up"
[21,] "L"  "meatball"     "deliver"
[22,] "L"  "meatball"     "pick-up"
[23,] "L"  "extra cheese" "deliver"
[24,] "L"  "extra cheese" "pick-up"
```

もっと良い方法は...

```R
> as.matrix(expand.grid(size, topping, order))
      Var1 Var2           Var3     
 [1,] "S"  "pepperoni"    "deliver"
 [2,] "M"  "pepperoni"    "deliver"
 [3,] "L"  "pepperoni"    "deliver"
 [4,] "S"  "sausage"      "deliver"
 [5,] "M"  "sausage"      "deliver"
 [6,] "L"  "sausage"      "deliver"
 [7,] "S"  "meatball"     "deliver"
 [8,] "M"  "meatball"     "deliver"
 [9,] "L"  "meatball"     "deliver"
[10,] "S"  "extra cheese" "deliver"
[11,] "M"  "extra cheese" "deliver"
[12,] "L"  "extra cheese" "deliver"
[13,] "S"  "pepperoni"    "pick-up"
[14,] "M"  "pepperoni"    "pick-up"
[15,] "L"  "pepperoni"    "pick-up"
[16,] "S"  "sausage"      "pick-up"
[17,] "M"  "sausage"      "pick-up"
[18,] "L"  "sausage"      "pick-up"
[19,] "S"  "meatball"     "pick-up"
[20,] "M"  "meatball"     "pick-up"
[21,] "L"  "meatball"     "pick-up"
[22,] "S"  "extra cheese" "pick-up"
[23,] "M"  "extra cheese" "pick-up"
[24,] "L"  "extra cheese" "pick-up"
```

でも，「並び順が気に入らない」という場合には...

```R
> as.matrix(expand.grid(order, topping, size)[3:1])
      Var3 Var2           Var1     
 [1,] "S"  "pepperoni"    "deliver"
 [2,] "S"  "pepperoni"    "pick-up"
 [3,] "S"  "sausage"      "deliver"
 [4,] "S"  "sausage"      "pick-up"
 [5,] "S"  "meatball"     "deliver"
 [6,] "S"  "meatball"     "pick-up"
 [7,] "S"  "extra cheese" "deliver"
 [8,] "S"  "extra cheese" "pick-up"
 [9,] "M"  "pepperoni"    "deliver"
[10,] "M"  "pepperoni"    "pick-up"
[11,] "M"  "sausage"      "deliver"
[12,] "M"  "sausage"      "pick-up"
[13,] "M"  "meatball"     "deliver"
[14,] "M"  "meatball"     "pick-up"
[15,] "M"  "extra cheese" "deliver"
[16,] "M"  "extra cheese" "pick-up"
[17,] "L"  "pepperoni"    "deliver"
[18,] "L"  "pepperoni"    "pick-up"
[19,] "L"  "sausage"      "deliver"
[20,] "L"  "sausage"      "pick-up"
[21,] "L"  "meatball"     "deliver"
[22,] "L"  "meatball"     "pick-up"
[23,] "L"  "extra cheese" "deliver"
[24,] "L"  "extra cheese" "pick-up"
```

expand.grid() はデータフレームを返すので，as.matrix() は最初の for ループが返す配列に合わせるために使っただけなので，データフレームでよいならば
as.matrix() する必要はない。