LoginSignup
0
0

More than 1 year has passed since last update.

Rでバスケット分析(arules)のためにtransaction型に変換しようと思ったら、不正なクラス “ngCMatrix” オブジェクト : row indices are not sorted within columnsが出た

Last updated at Posted at 2022-06-24

バスケット分析を行おうと、transaction型に変換しようとしたんですが、エラーが起きてしまいました。

> as(
+   list(
+     "basket1" = c(
+       "SDGs"
+     ),
+     "basket2" = c(
+       "SDGs" # sが全角
+       , "SDGs"
+     ),
+     "basket3" = c(
+       "あさがお"
+     )
+     "basket4" = c(
+       "あさがお"
+       , "アサガオ"
+     )
+   ),
+   "transactions"
+ )
 validObject(.Object) でエラー: 
   不正なクラス ngCMatrix オブジェクト : row indices are not sorted within columns

原因はこれだけでないでしょうが、一応私がハマったエラーを記載しておきます

原因1: 全角・半角が併存している。

下記のようにSDGs SDSs(sは全角)が併存しているときに、他のバスケットにSDGsが存在していたらエラーになるようです。
半角表記したときに同じ語になるワードが併存するのが鬼門みたいです。

as(
  list(
    "basket1" = c(
      "SDGs"
    ),
    "basket2" = c(
      "SDGs" # sが全角
      , "SDGs"
    )
  ),
  "transactions"
)

私の場合、BigQueryで元データを取得していたので、以下の方法で半角表記に寄せました
https://itips.krsw.biz/bigquery-how-to-normalize-multi-byte-string-text/

原因2: ひらがな・カタカナが併存している

原因1のひらがな・カタカナ併存バージョンです。

as(
  list(
    "basket3" = c(
      "あさがお"
    )
    "basket4" = c(
      "あさがお"
      , "アサガオ"
    )
  ),
  "transactions"
)

気持ち悪いですが、カタカナに寄せることで解決しました
https://sem-aa-bq.hatenablog.com/entry/2019/03/02/010721

原因3: 0(ゼロ)と○(丸印)が併存している

as(
  list(
    "basket3" = c(
      "0円"
    )
    "basket4" = c(
      "0円"
      , "○円"
    )
  ),
  "transactions"
)

これは単純に置換すれば大丈夫です

原因4: ゼロ幅スペースがある

詳しくはこちらをご覧ください。

注記

バスケット内に同じ要素が入っている場合、重複要素が削除されます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0