[`dplyr::between()`](https://dplyr.tidyverse.org/reference/between.html) は数値データに特化して設計されており、順序ファクタの範囲指定には対応していない。 ```r diamonds |> filter(between(cut, "Good", "Premium")) |> distinct(cut) ``` ``` # A tibble: 3 × 1 cut <ord> 1 Ideal 2 Premium 3 Good ``` `diamonds$cut` の順序 (`Fair < Good < Very Good < Premium < Ideal`) を考慮すると、期待される結果は `Good`、`Very Good`、`Premium` の 3 つである。 ところが実際の結果は、`Ideal` を含んだり `Very Good` を欠いたりと、この順序を無視しており正常に動作していない。 ## 正しい解決方法 順序ファクタの範囲指定には、比較演算子 (`>=`, `<=`) を使用する。 ```r diamonds |> filter(cut >= "Good" & cut <= "Premium") |> distinct(cut) ``` ``` # A tibble: 3 × 1 cut <ord> 1 Premium 2 Good 3 Very Good ``` ## 関連ノート - **[前提]** [[dplyr の filter() で閉区間の範囲指定をショートカットする]]: 本ノートは、この `between()` が順序ファクタ型には適用できないという例外ケースを扱う