Home > OS >  Error when extracting column indices with dplyr and conditional pipe operator evaluation
Error when extracting column indices with dplyr and conditional pipe operator evaluation

Time:03-10

The following two operations should lead both to the indices of all numeric variables of the mtcars dataset. Nevertheless, the operation which uses the conditional evaluation of the pipe operator for the dataset produces an error. Can anyone explain why?

MWE :

data(mtcars)
require(dplyr)
require(stringr)
mtcars %>% {grep(pattern = . %>% select_if(.predicate = is.numeric ) %>% 
                              colnames %>%
                              str_flatten('|'),x = colnames(.))}

Output :

Error in as.vector(x, "character") : cannot coerce type 'closure' to vector of type 'character'

data(mtcars)
require(dplyr)
require(stringr)
grep(pattern = mtcars %>% select_if(.predicate = is.numeric ) %>% 
                                  colnames %>%
                                  str_flatten('|'),x = colnames(mtcars))

Delivers the expected output : [1] 1 2 3 4 5 6 7 8 9 10 11

Session info

> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stringr_1.4.0 dplyr_1.0.7  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7        rstudioapi_0.13   magrittr_2.0.1    tidyselect_1.1.1  munsell_0.5.0     colorspace_1.4-1  R6_2.5.1          rlang_0.4.11.9001 fansi_0.5.0       tools_4.0.5       grid_4.0.5       
[12] gtable_0.3.0      GauPro_0.2.4      utf8_1.2.2        cli_3.0.1         DBI_1.1.0         ellipsis_0.3.2    assertthat_0.2.1  tibble_3.1.5      lifecycle_1.0.1   crayon_1.4.1      purrr_0.3.4      
[23] ggplot2_3.3.5     vctrs_0.3.8       rpart_4.1-15      glue_1.4.2        stringi_1.4.6     compiler_4.0.5    pillar_1.6.4      generics_0.1.0    scales_1.1.1      pkgconfig_2.0.3 

CodePudding user response:

We may use the pipe with where in select as _if/_at are deprecated

library(dplyr)
library(stringr)
mtcars %>% 
 select(where(is.numeric)) %>% 
 names %>%
 str_flatten('|') %>%
 grep(x = colnames(mtcars))

-output

[1]  1  2  3  4  5  6  7  8  9 10 11

As we are using stringr, str_which would give the same index output as grep

mtcars %>% 
  select(where(is.numeric)) %>% 
  names %>%
  str_flatten('|') %>%
  str_which(string = names(mtcars))

-output

[1]  1  2  3  4  5  6  7  8  9 10 11

In the OP's code, the . can be wrapped within {}

mtcars %>%
   {grep(pattern = {.} %>% 
           select_if(.predicate = is.numeric ) %>% 
           colnames %>%
           str_flatten('|'),x = colnames(.))
    }

-output

[1]  1  2  3  4  5  6  7  8  9 10 11
  • Related