R でベクトルの組み合わせと順列を作成する

Jesse John 2023年6月21日
  1. 二項係数と組み合わせ
  2. 階乗と順列
  3. 1つのベクトルの要素の組み合わせ
  4. 多くのベクトルの要素の組み合わせ
  5. ベクトルの要素の順列
  6. 一意でない要素の処理
  7. まとめ
R でベクトルの組み合わせと順列を作成する

Base R は、二項係数を計算し、順列の数を見つけるのに役立つ関数を提供します。 ただし、番号を取得するだけでなく、すべての組み合わせと順列を見つけたい場合があります。

この記事では、組み合わせと順列の数を取得する方法と、すべての組み合わせと順列を取得する方法について説明します。 また、ベクトルに重複要素がある場合に何が起こるかについても説明します。

これらの関数は、限られた範囲の値に対してのみ機能することに注意してください。

二項係数と組み合わせ

choose(n,k) 関数は二項係数を計算します。 繰り返しなしで一度に k 取得された n 個のユニークなアイテムの組み合わせの数を取得します。

コード例:

# 組み合わせ
# 20 個のオブジェクトから 2つのオブジェクトの組み合わせを数えます
選択(20,2)

# 値が非常に大きい場合は機能しません
選ぶ(1030,500)

出力:

> # Count combinations of 2 objects from 20 objects.
> choose(20,2)
[1] 190

> # It does not work for very large values.
> choose(1030,500)
[1] Inf

階乗と順列

factorial(x) 関数は、繰り返しのない x の一意のオブジェクトの順列の数を示します。 n オブジェクトのセットから k オブジェクトの順列をカウントするためのベース R には特定の関数はありませんが、factorial(n)factorial(k) の商として計算できます。

コード例:

# 順列
# 17 個の異なるオブジェクトの順列
階乗(17)

# 大きな値では機能しません
階乗(171)

# 19 個の異なるオブジェクトからの 5 個のオブジェクトの順列
factorial(19)/factorial(5)

出力:

> # Permutations of 17 distinct objects.
> factorial(17)
[1] 3.556874e+14

> # It does not work for large values.
> factorial(171)
[1] Inf

> # Permutations of 5 objects from 19 distinct objects.
> factorial(19)/factorial(5)
[1] 1.013709e+15

1つのベクトルの要素の組み合わせ

関数 combn(x,m) は、x の異なるオブジェクトから抽出された m オブジェクトのすべての組み合わせを繰り返しなしでリストします。 列はさまざまな組み合わせです。

コード例:

# オブジェクトのベクトル
mv1 = c("テニス", "バドミントン", "サッカー", "陸上競技", "チェス")
mv1

# 2つの要素のすべての組み合わせ
くし(mv1,2)

# 三大元素のすべての組み合わせ
くし(mv1,3)

# プロパティをチェック
c3 = 櫛 (mv1,3)
クラス(c3)
名前(c3)

出力:

> mv1
[1] "Tennis"    "Badminton" "Football"  "Athletics" "Chess"

> # All combinations of two elements.
> combn(mv1,2)
     [,1]        [,2]       [,3]        [,4]     [,5]        [,6]        [,7]        [,8]        [,9]
[1,] "Tennis"    "Tennis"   "Tennis"    "Tennis" "Badminton" "Badminton" "Badminton" "Football"  "Football"
[2,] "Badminton" "Football" "Athletics" "Chess"  "Football"  "Athletics" "Chess"     "Athletics" "Chess"
     [,10]
[1,] "Athletics"
[2,] "Chess"

> # Check its properties.
> c3 = combn(mv1,3)

> class(c3)
[1] "matrix" "array"

> names(c3)
NULL

出力は配列です。 列または行の名前はありません。

多くのベクトルの要素の組み合わせ

expand.grid() 関数を使用すると、指定されたベクトルの要素のすべての組み合わせでデータ フレームを作成できます。 この関数には、複数のベクトルを入力として指定できます。

繰り返しますが、関数が目的の出力を与えるには、入力ベクトルに個別の要素が必要です。

コード例:

# 新しいベクターを作成
mv2 = c("りんご", "マンゴー", "にんじん")
mv2

# expand.grid() 関数は、指定されたベクトルの要素のすべての組み合わせを一覧表示する
# 既存のベクターを使用するか、C() を使用してベクターを作成する
ex_df = expand.grid(Sport = mv1, Fruit = mv2, Color = c("青", "赤"))
ex_df

出力:

> mv2
[1] "Apple"  "Mango"  "Carrot"

> # The expand.grid() function lists all combinations of the elements of the given vectors.
> # Use existing vectors or create a vector using c().
> ex_df = expand.grid(Sport = mv1, Fruit = mv2, Color = c("Blue", "Red"))
> ex_df
       Sport  Fruit Color
1     Tennis  Apple  Blue
2  Badminton  Apple  Blue
3   Football  Apple  Blue
4  Athletics  Apple  Blue
5      Chess  Apple  Blue
6     Tennis  Mango  Blue
7  Badminton  Mango  Blue
8   Football  Mango  Blue
9  Athletics  Mango  Blue
10     Chess  Mango  Blue
11    Tennis Carrot  Blue
12 Badminton Carrot  Blue
13  Football Carrot  Blue
14 Athletics Carrot  Blue
15     Chess Carrot  Blue
16    Tennis  Apple   Red
17 Badminton  Apple   Red
18  Football  Apple   Red
19 Athletics  Apple   Red
20     Chess  Apple   Red
21    Tennis  Mango   Red
22 Badminton  Mango   Red
23  Football  Mango   Red
24 Athletics  Mango   Red
25     Chess  Mango   Red
26    Tennis Carrot   Red
27 Badminton Carrot   Red
28  Football Carrot   Red
29 Athletics Carrot   Red
30     Chess Carrot   Red

列がベクトルに対応し、行が要素の組み合わせであるデータ フレームを取得します。 行数は、各ベクトルの要素数の積です。

ベクトルの要素の順列

combinat パッケージの permn() 関数は、ベクトルのすべての要素の順列を作成します。 このパッケージをインストールする必要がある場合があります。

コード例:

# Combinat パッケージが利用できない場合はインストールする
# コメントを外して次の行を実行し、インストールする
# install.packages("combinat")

# Combinat パッケージをロードする
ライブラリ(コンビナト)

# ベクトルのすべての順列を作成
パーマ(mv2)

出力:

> # Create all permutations of a vector.
> permn(mv2)
[[1]]
[1] "Apple"  "Mango"  "Carrot"

[[2]]
[1] "Apple"  "Carrot" "Mango"

[[3]]
[1] "Carrot" "Apple"  "Mango"

[[4]]
[1] "Carrot" "Mango"  "Apple"

[[5]]
[1] "Mango"  "Carrot" "Apple"

[[6]]
[1] "Mango"  "Apple"  "Carrot"

一意でない要素の処理

ベクトルのいくつかの要素が同一である場合に何が起こるか見てみましょう。 3つの関数すべての出力を見ていきます。

コード例:

# Combn() の例
# いくつかの同一要素を持つベクトル
mv3 = c("テニス", "テニス", "サッカー", "陸上競技")
mv3
# 結果の組み合わせ
c2 = 櫛 (mv3,2)
c2

# expand.grid() の例
mv4 = c("赤", "青", "赤")
例 = expand.grid(Sport = mv3, Color = mv4)
例えば

# Permn() の例
pn = パーマ (mv4)
PN

出力:

# Example for combn().
> mv3
[1] "Tennis"    "Tennis"    "Football"  "Athletics"
> # Resulting combinations.
> c2 = combn(mv3,2)
> c2
     [,1]     [,2]       [,3]        [,4]       [,5]        [,6]
[1,] "Tennis" "Tennis"   "Tennis"    "Tennis"   "Tennis"    "Football"
[2,] "Tennis" "Football" "Athletics" "Football" "Athletics" "Athletics"

> # Example for expand.grid().
> mv4 = c("Red", "Blue", "Red")
> eg = expand.grid(Sport = mv3, Color = mv4)
> eg
       Sport Color
1     Tennis   Red
2     Tennis   Red
3   Football   Red
4  Athletics   Red
5     Tennis  Blue
6     Tennis  Blue
7   Football  Blue
8  Athletics  Blue
9     Tennis   Red
10    Tennis   Red
11  Football   Red
12 Athletics   Red

> # Example for permn().
> pn = permn(mv4)
> pn
[[1]]
[1] "Red"  "Blue" "Red"

[[2]]
[1] "Red"  "Red"  "Blue"

[[3]]
[1] "Red"  "Red"  "Blue"

[[4]]
[1] "Red"  "Blue" "Red"

[[5]]
[1] "Blue" "Red"  "Red"

[[6]]
[1] "Blue" "Red"  "Red"

combn() 出力では、同一の要素が他の要素と結合されます。 expand.grid() の出力には同一の行があり、同一の要素が permn() の出力の各行に表示され、いくつかの行も同一です。

これが必要な出力でない場合は、さらにデータ処理を行う必要があります。 一般に、最も簡単なオプションは、使用するベクトルが一意の要素を持つようにすることです。この目的には unique() 関数を使用できます。

コード例:

# 重複を削除した後の出力

# Combn()
c2_u = combn(ユニーク(mv3),2)
c2_u

# expand.grid()
eg_u = expand.grid(Sport = unique(mv3), Color = unique(mv4))
eg_u

#パーマ()
pn_u = permn (ユニーク (mv4))
pn_u

出力:

> # combn()
> c2_u = combn(unique(mv3),2)
> c2_u
     [,1]       [,2]        [,3]
[1,] "Tennis"   "Tennis"    "Football"
[2,] "Football" "Athletics" "Athletics"

> # expand.grid()
> eg_u = expand.grid(Sport = unique(mv3), Color = unique(mv4))
> eg_u
      Sport Color
1    Tennis   Red
2  Football   Red
3 Athletics   Red
4    Tennis  Blue
5  Football  Blue
6 Athletics  Blue

> # permn()
> pn_u = permn(unique(mv4))
> pn_u
[[1]]
[1] "Red"  "Blue"

[[2]]
[1] "Blue" "Red"

まとめ

分析やイラスト用のデータを作成する必要がある場合、考えられるすべての要素の組み合わせを 1つまたは複数のベクトルで作成できると便利です。

この記事では、R でベクトルの組み合わせと順列を生成し、そのような組み合わせと順列の数を計算することがいかに簡単かを説明しました。

また、ベクトル内の同一要素の影響を調べ、問題に対処する 1つの方法を見てきました。

著者: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.

関連記事 - R Vector