3 min read

二項演算子 %in% を改良したかった (R)

## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'agricolae'
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'slackr'

%in%をよく使う

iris %>%
  filter(Species %in% "setosa") %>%
  head
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

data.table::betweenもよく使う

# rows 4.5 ≤ Petal.Length ≤ 5.5
iris %>%
  filter(between(Petal.Length, 4.5, 5.5)) %>%
  head
##   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1          7.0         3.2          4.7         1.4 versicolor
## 2          6.4         3.2          4.5         1.5 versicolor
## 3          6.9         3.1          4.9         1.5 versicolor
## 4          6.5         2.8          4.6         1.5 versicolor
## 5          5.7         2.8          4.5         1.3 versicolor
## 6          6.3         3.3          4.7         1.6 versicolor
# rows !(4.5 ≤ Petal.Length ≤ 5.5)
iris %>%
  filter(!between(Petal.Length, 4.5, 5.5)) %>%
  head
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

排反を取得したいときにこんな感じで書きたくなる
が、エラーを吐かれる

iris %>%
  filter(Species !%in% "setosa")

Error: unexpected '!' in:
"iris %>%
  filter(Species !"

%in%の背反をとるにはこう書く

iris %>%
  filter(!(Species %in% "setosa")) %>%
  head
##   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1          7.0         3.2          4.7         1.4 versicolor
## 2          6.4         3.2          4.5         1.5 versicolor
## 3          6.9         3.1          4.9         1.5 versicolor
## 4          5.5         2.3          4.0         1.3 versicolor
## 5          6.5         2.8          4.6         1.5 versicolor
## 6          5.7         2.8          4.5         1.3 versicolor

直感的ではない(ように思う)

!%in%を実装したかった

%in%の中身

# バッククオートで囲むと参照できる
`%in%`
## function (x, table) 
## match(x, table, nomatch = 0L) > 0L
## <bytecode: 0x7ff0c30397c0>
## <environment: namespace:base>

!%in%を素直に試みる

`!%in%` <-
  function (x, table){
    !match(x, table, nomatch = 0L) > 0L
  }

# 関数作成はできる
`!%in%` %>%
  print

# 実行はできない
iris %>%
  filter(Species !%in% "setosa")

Error: unexpected '!' in:
"iris %>%
  filter(Species !"

できなかった
ので、代替品を作っておいた

`%!in%` <-
  function (x, table){
    !match(x, table, nomatch = 0L) > 0L
  }

iris %>%
  filter(Species %!in% "setosa") %>%
  head
##   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1          7.0         3.2          4.7         1.4 versicolor
## 2          6.4         3.2          4.5         1.5 versicolor
## 3          6.9         3.1          4.9         1.5 versicolor
## 4          5.5         2.3          4.0         1.3 versicolor
## 5          6.5         2.8          4.6         1.5 versicolor
## 6          5.7         2.8          4.5         1.3 versicolor
session_info()
##  setting  value                       
##  version  R version 3.5.0 (2018-04-23)
##  system   x86_64, darwin15.6.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       Australia/Brisbane          
##  date     2018-05-16                  
## 
##  package      * version   date       source                          
##  assertthat     0.2.0     2017-04-11 CRAN (R 3.5.0)                  
##  backports      1.1.2     2017-12-13 CRAN (R 3.5.0)                  
##  base         * 3.5.0     2018-04-24 local                           
##  bindr          0.1.1     2018-03-13 CRAN (R 3.5.0)                  
##  bindrcpp     * 0.2.2     2018-03-29 CRAN (R 3.5.0)                  
##  bitops       * 1.0-6     2013-08-17 CRAN (R 3.5.0)                  
##  blogdown       0.6       2018-04-18 CRAN (R 3.5.0)                  
##  bookdown       0.7       2018-02-18 CRAN (R 3.5.0)                  
##  codetools      0.2-15    2016-10-05 CRAN (R 3.5.0)                  
##  colorspace     1.3-2     2016-12-14 CRAN (R 3.5.0)                  
##  compiler       3.5.0     2018-04-24 local                           
##  data.table   * 1.10.4-3  2017-10-27 CRAN (R 3.5.0)                  
##  datasets     * 3.5.0     2018-04-24 local                           
##  devtools     * 1.13.5    2018-02-18 CRAN (R 3.5.0)                  
##  digest         0.6.15    2018-01-28 CRAN (R 3.5.0)                  
##  doParallel     1.0.11    2017-09-28 CRAN (R 3.5.0)                  
##  doRNG          1.6.6     2017-04-10 CRAN (R 3.5.0)                  
##  dplyr        * 0.7.4     2017-09-28 CRAN (R 3.5.0)                  
##  evaluate       0.10.1    2017-06-24 CRAN (R 3.5.0)                  
##  foreach      * 1.4.4     2017-12-12 CRAN (R 3.5.0)                  
##  ggplot2      * 2.2.1     2016-12-30 CRAN (R 3.5.0)                  
##  glue           1.2.0     2017-10-29 CRAN (R 3.5.0)                  
##  graphics     * 3.5.0     2018-04-24 local                           
##  grDevices    * 3.5.0     2018-04-24 local                           
##  grid         * 3.5.0     2018-04-24 local                           
##  gridExtra    * 2.3       2017-09-09 CRAN (R 3.5.0)                  
##  gtable       * 0.2.0     2016-02-26 CRAN (R 3.5.0)                  
##  htmltools      0.3.6     2017-04-28 CRAN (R 3.5.0)                  
##  iterators      1.0.9     2017-12-12 CRAN (R 3.5.0)                  
##  knitr        * 1.20      2018-02-20 CRAN (R 3.5.0)                  
##  lazyeval       0.2.1     2017-10-29 CRAN (R 3.5.0)                  
##  lubridate    * 1.7.4     2018-04-11 CRAN (R 3.5.0)                  
##  magrittr     * 1.5       2014-11-22 CRAN (R 3.5.0)                  
##  MASS         * 7.3-49    2018-02-23 CRAN (R 3.5.0)                  
##  memoise        1.1.0     2017-04-21 CRAN (R 3.5.0)                  
##  methods      * 3.5.0     2018-04-24 local                           
##  munsell        0.4.3     2016-02-13 CRAN (R 3.5.0)                  
##  parallel       3.5.0     2018-04-24 local                           
##  pforeach     * 1.3       2018-04-25 Github (hoxo-m/pforeach@2c44f3b)
##  pillar         1.2.1     2018-02-27 CRAN (R 3.5.0)                  
##  pkgconfig      2.0.1     2017-03-21 CRAN (R 3.5.0)                  
##  pkgmaker       0.22      2014-05-14 CRAN (R 3.5.0)                  
##  plyr         * 1.8.4     2016-06-08 CRAN (R 3.5.0)                  
##  purrr          0.2.4     2017-10-18 CRAN (R 3.5.0)                  
##  R6             2.2.2     2017-06-17 CRAN (R 3.5.0)                  
##  RColorBrewer * 1.1-2     2014-12-07 CRAN (R 3.5.0)                  
##  Rcpp           0.12.16   2018-03-13 CRAN (R 3.5.0)                  
##  RCurl        * 1.95-4.10 2018-01-04 CRAN (R 3.5.0)                  
##  registry       0.5       2017-12-03 CRAN (R 3.5.0)                  
##  reshape2     * 1.4.3     2017-12-11 CRAN (R 3.5.0)                  
##  rlang          0.2.0     2018-02-20 CRAN (R 3.5.0)                  
##  rmarkdown      1.9       2018-03-01 CRAN (R 3.5.0)                  
##  rngtools       1.2.4     2014-03-06 CRAN (R 3.5.0)                  
##  rprojroot      1.3-2     2018-01-03 CRAN (R 3.5.0)                  
##  scales       * 0.5.0     2017-08-24 CRAN (R 3.5.0)                  
##  stats        * 3.5.0     2018-04-24 local                           
##  stringi        1.2.2     2018-05-02 cran (@1.2.2)                   
##  stringr      * 1.3.1     2018-05-10 cran (@1.3.1)                   
##  tibble         1.4.2     2018-01-22 CRAN (R 3.5.0)                  
##  tidyr        * 0.8.0     2018-01-29 CRAN (R 3.5.0)                  
##  tools          3.5.0     2018-04-24 local                           
##  utils        * 3.5.0     2018-04-24 local                           
##  withr          2.1.2     2018-03-15 CRAN (R 3.5.0)                  
##  xfun           0.1       2018-01-22 CRAN (R 3.5.0)                  
##  xtable         1.8-2     2016-02-05 CRAN (R 3.5.0)                  
##  yaml           2.1.18    2018-03-08 CRAN (R 3.5.0)