arulesパッケージのデータセット(食料品)をデータフレームに変換する方法。トランザクションオブジェクトをRのデータフレームに変換する方法
class(Groceries)
[1] "transactions"
attr(,"package")
[1] "arules"
arulesパッケージのデータセット(食料品)をデータフレームに変換する方法。トランザクションオブジェクトをRのデータフレームに変換する方法
class(Groceries)
[1] "transactions"
attr(,"package")
[1] "arules"
実際に何が必要なのかを指定する必要があります。 arules
-packageのヘルプページにas.data.frame.transactions
機能が表示されません。実際たい
str(Groceries)
Formal class 'transactions' [package "arules"] with 3 slots
[email protected] data :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
.. .. [email protected] i : int [1:43367] 13 60 69 78 14 29 98 24 15 29 ...
.. .. [email protected] p : int [1:9836] 0 4 7 8 12 16 21 22 27 28 ...
.. .. [email protected] Dim : int [1:2] 169 9835
.. .. [email protected] Dimnames:List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : NULL
.. .. [email protected] factors : list()
[email protected] itemInfo :'data.frame': 169 obs. of 3 variables:
.. ..$ labels: chr [1:169] "frankfurter" "sausage" "liver loaf" "ham" ...
.. ..$ level2: Factor w/ 55 levels "baby food","bags",..: 44 44 44 44 44 44 44 42 42 41 ...
.. ..$ level1: Factor w/ 10 levels "canned food",..: 6 6 6 6 6 6 6 6 6 6 ...
[email protected] itemsetInfo:'data.frame': 0 obs. of 0 variables
私はあなたを推測している:データ項目Groceries
は、その中に埋め込まれたデータフレームを持っているが、それはあなたが望むものであるかどうかは考えにくい
as.matrix([email protected])
おそらくrownamesとして[email protected] itemInfo$ labels
を追加します
arules
はS4タイプのオブジェクトを使用しますが、トランザクションデータは転置スパース行列に格納されます。 @
を使用してデータに直接アクセスすべきではありません。強制的にトランザクションデータにアクセスします。これにより、データが正しい項目ラベルで適切な形式になっていることが確認されます。
例:
as(Groceries, "matrix")
as(Groceries, "list")
マトリックスは論理トランザクション・バイ・アイテム行列であり、リストは、トランザクション・セットのリストを生成します。詳細は、の方法を?transactions
に見てください。
これは、最初に行列を作成し、次にデータフレームに追加し、Colnamesを追加するのに役立ちます:) –