Mutate_if Internal Error expected a vector

dplyr

#1

I’ve been hitting my head on what might be a bug or PEBCAK. I am trying to select columns for mutation by name and then divide by another column. Here is an example of the exact code I’m using with the iris dataset.

I should add that this used to work, so I’m guessing it’s due to a recent update in dplyr or related package.


library(dplyr)

mut_names = c("Sepal.Length")

iris %>% mutate_if(names(.) %in% mut_names, funs(./Sepal.Width))

Error: Internal error: expected a vector

devtools::session_info()
Session info ------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.2 (2017-09-28)
 system   x86_64, darwin15.6.0        
 ui       AQUA                        
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/Los_Angeles         
 date     2017-12-08                  

Packages ----------------------------------------------------------------------------------------------
 package    * version    date       source                          
 assertthat   0.2.0      2017-04-11 CRAN (R 3.4.0)                  
 base       * 3.4.2      2017-10-04 local                           
 bindr        0.1        2016-11-13 cran (@0.1)                     
 bindrcpp     0.2        2017-06-17 cran (@0.2)                     
 compiler     3.4.2      2017-10-04 local                           
 datasets   * 3.4.2      2017-10-04 local                           
 devtools     1.13.0     2017-05-08 CRAN (R 3.4.0)                  
 digest       0.6.12     2017-01-27 CRAN (R 3.4.0)                  
 dplyr      * 0.7.4.9000 2017-11-28 Github (tidyverse/dplyr@fc66342)
 glue         1.2.0      2017-10-29 cran (@1.2.0)                   
 graphics   * 3.4.2      2017-10-04 local                           
 grDevices  * 3.4.2      2017-10-04 local                           
 magrittr     1.5        2014-11-22 CRAN (R 3.4.0)                  
 memoise      1.1.0      2017-04-21 CRAN (R 3.4.0)                  
 methods    * 3.4.2      2017-10-04 local                           
 pkgconfig    2.0.1      2017-03-21 cran (@2.0.1)                   
 purrr        0.2.4.9000 2017-11-28 Github (tidyverse/purrr@62b135a)
 R6           2.2.2      2017-06-17 cran (@2.2.2)                   
 Rcpp         0.12.14    2017-11-23 cran (@0.12.14)                 
 rlang        0.1.4.9000 2017-11-28 Github (tidyverse/rlang@bd83400)
 stats      * 3.4.2      2017-10-04 local                           
 tibble       1.3.4      2017-08-22 cran (@1.3.4)                   
 tidyselect   0.2.3      2017-11-06 cran (@0.2.3)                   
 utils      * 3.4.2      2017-10-04 local                           
 withr        2.1.0.9000 2017-11-18 Github (jimhester/withr@daf5a8c)

#2

Well, I reinstalled Tidyverse and it seems to have fixed.

Differences in session_info() suggest changes in rlang or withr could be the culprit.

devtools::session_info()
Session info ------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.2 (2017-09-28)
 system   x86_64, darwin15.6.0        
 ui       AQUA                        
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/Los_Angeles         
 date     2017-12-08                  

Packages ----------------------------------------------------------------------------------------------
 package    * version    date       source                          
 assertthat   0.2.0      2017-04-11 CRAN (R 3.4.0)                  
 base       * 3.4.2      2017-10-04 local                           
 bindr        0.1        2016-11-13 cran (@0.1)                     
 bindrcpp   * 0.2        2017-06-17 cran (@0.2)                     
 compiler     3.4.2      2017-10-04 local                           
 datasets   * 3.4.2      2017-10-04 local                           
 devtools     1.13.0     2017-05-08 CRAN (R 3.4.0)                  
 digest       0.6.12     2017-01-27 CRAN (R 3.4.0)                  
 dplyr      * 0.7.4.9000 2017-11-28 Github (tidyverse/dplyr@fc66342)
 glue         1.2.0      2017-10-29 cran (@1.2.0)                   
 graphics   * 3.4.2      2017-10-04 local                           
 grDevices  * 3.4.2      2017-10-04 local                           
 magrittr     1.5        2014-11-22 CRAN (R 3.4.0)                  
 memoise      1.1.0      2017-04-21 CRAN (R 3.4.0)                  
 methods    * 3.4.2      2017-10-04 local                           
 pkgconfig    2.0.1      2017-03-21 cran (@2.0.1)                   
 purrr        0.2.4.9000 2017-11-28 Github (tidyverse/purrr@62b135a)
 R6           2.2.2      2017-06-17 cran (@2.2.2)                   
 Rcpp         0.12.14    2017-11-23 cran (@0.12.14)                 
 rlang        0.1.4.9000 2017-12-08 Github (tidyverse/rlang@5ebcf24)
 stats      * 3.4.2      2017-10-04 local                           
 tibble       1.3.4      2017-08-22 cran (@1.3.4)                   
 tidyselect   0.2.3      2017-11-06 cran (@0.2.3)                   
 utils      * 3.4.2      2017-10-04 local                           
 withr        2.1.0.9000 2017-12-08 Github (jimhester/withr@fe81c00)

#3

You could also use mutate_at() and get the same results:

library(dplyr)

mut_names <- c("Sepal.Length")

iris %>% mutate_at(mut_names, funs(. / Sepal.Width)) %>%
  as_tibble() # for pretty printing

#> # A tibble: 150 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl>  <fctr>
#>  1     1.457143         3.5          1.4         0.2  setosa
#>  2     1.633333         3.0          1.4         0.2  setosa
#>  3     1.468750         3.2          1.3         0.2  setosa
#>  4     1.483871         3.1          1.5         0.2  setosa
#>  5     1.388889         3.6          1.4         0.2  setosa
#>  6     1.384615         3.9          1.7         0.4  setosa
#>  7     1.352941         3.4          1.4         0.3  setosa
#>  8     1.470588         3.4          1.5         0.2  setosa
#>  9     1.517241         2.9          1.4         0.2  setosa
#> 10     1.580645         3.1          1.5         0.1  setosa
#> # ... with 140 more rows

#4

Indeed. That’s a good change. Forgot that *_at accept character column names as input. Certainly makes it more simple @mfherman


#5

I think I’m hitting the same thing, and updating tidyverse doesn’t fix for me.

library(tidyverse)
# Works:
iris %>%
  mutate_if(is.factor, as.character)

# Doesn't work:
fcts <- map_lgl(iris, is.factor)
iris %>%
  mutate_if(fcts, as.character)
# Error: Internal error: expected a vector
> devtools::session_info()
Session info -------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.2 (2017-09-28)
 system   x86_64, darwin15.6.0        
 ui       RStudio (1.1.383)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/Denver              
 date     2017-12-13                  

Packages -----------------------------------------------------------------------------------------------------------------------
 package    * version    date       source                             
 assertthat   0.2.0      2017-04-11 CRAN (R 3.4.0)                     
 base       * 3.4.2      2017-10-04 local                              
 bindr        0.1        2016-11-13 CRAN (R 3.4.0)                     
 bindrcpp   * 0.2        2017-06-17 CRAN (R 3.4.0)                     
 broom        0.4.2      2017-02-13 CRAN (R 3.4.0)                     
 cellranger   1.1.0      2016-07-27 CRAN (R 3.4.0)                     
 cli          1.0.0      2017-11-05 CRAN (R 3.4.2)                     
 colorspace   1.3-2      2016-12-14 CRAN (R 3.4.0)                     
 compiler     3.4.2      2017-10-04 local                              
 crayon       1.3.4      2017-11-03 Github (gaborcsardi/crayon@b5221ab)
 datasets   * 3.4.2      2017-10-04 local                              
 devtools     1.13.3     2017-08-02 CRAN (R 3.4.1)                     
 digest       0.6.12     2017-01-27 CRAN (R 3.4.0)                     
 dplyr      * 0.7.4      2017-09-28 CRAN (R 3.4.2)                     
 forcats    * 0.2.0      2017-01-23 CRAN (R 3.4.0)                     
 foreign      0.8-69     2017-06-22 CRAN (R 3.4.2)                     
 ggplot2    * 2.2.1      2016-12-30 CRAN (R 3.4.0)                     
 glue         1.2.0      2017-10-29 cran (@1.2.0)                      
 graphics   * 3.4.2      2017-10-04 local                              
 grDevices  * 3.4.2      2017-10-04 local                              
 grid         3.4.2      2017-10-04 local                              
 gtable       0.2.0      2016-02-26 CRAN (R 3.4.0)                     
 haven        1.1.0      2017-07-09 CRAN (R 3.4.1)                     
 hms          0.3        2016-11-22 CRAN (R 3.4.0)                     
 httr         1.3.1      2017-08-20 CRAN (R 3.4.1)                     
 jsonlite     1.5        2017-06-01 CRAN (R 3.4.0)                     
 lattice      0.20-35    2017-03-25 CRAN (R 3.4.2)                     
 lazyeval     0.2.0      2016-06-12 CRAN (R 3.4.0)                     
 lubridate    1.7.1      2017-11-03 cran (@1.7.1)                      
 magrittr     1.5        2014-11-22 CRAN (R 3.4.0)                     
 memoise      1.1.0      2017-04-21 CRAN (R 3.4.0)                     
 methods    * 3.4.2      2017-10-04 local                              
 mnormt       1.5-5      2016-10-15 CRAN (R 3.4.0)                     
 modelr       0.1.1      2017-07-24 CRAN (R 3.4.1)                     
 munsell      0.4.3      2016-02-13 CRAN (R 3.4.0)                     
 nlme         3.1-131    2017-02-06 CRAN (R 3.4.2)                     
 parallel     3.4.2      2017-10-04 local                              
 pkgconfig    2.0.1      2017-03-21 CRAN (R 3.4.0)                     
 plyr         1.8.4      2016-06-08 CRAN (R 3.4.0)                     
 psych        1.7.8      2017-09-09 CRAN (R 3.4.0)                     
 purrr      * 0.2.4      2017-10-18 cran (@0.2.4)                      
 R6           2.2.2      2017-06-17 CRAN (R 3.4.0)                     
 Rcpp         0.12.13    2017-09-28 CRAN (R 3.4.2)                     
 readr      * 1.1.1      2017-05-16 CRAN (R 3.4.0)                     
 readxl       1.0.0      2017-04-18 CRAN (R 3.4.0)                     
 reshape2     1.4.2      2016-10-22 CRAN (R 3.4.0)                     
 rlang        0.1.4.9000 2017-11-16 Github (tidyverse/rlang@1e54041)   
 rstudioapi   0.7        2017-09-07 CRAN (R 3.4.1)                     
 rvest        0.3.2      2016-06-17 CRAN (R 3.4.0)                     
 scales       0.5.0      2017-08-24 CRAN (R 3.4.1)                     
 stats      * 3.4.2      2017-10-04 local                              
 stringi      1.1.5      2017-04-07 CRAN (R 3.4.0)                     
 stringr    * 1.2.0      2017-02-18 CRAN (R 3.4.0)                     
 tibble     * 1.3.4      2017-08-22 cran (@1.3.4)                      
 tidyr      * 0.7.2      2017-10-16 CRAN (R 3.4.2)                     
 tidyverse  * 1.2.1      2017-11-14 CRAN (R 3.4.2)                     
 tools        3.4.2      2017-10-04 local                              
 utils      * 3.4.2      2017-10-04 local                              
 withr        2.1.0      2017-11-01 cran (@2.1.0)                      
 xml2         1.1.1      2017-01-24 CRAN (R 3.4.0)                     
 yaml         2.1.14     2016-11-12 CRAN (R 3.4.0)     

#6

Mike,

That’s definitely the same thing. Logical vector input to mutate_if() expecting a vector.

I can no longer make it happen though. With your example on my machine, I get the same result as the first attempt.

From your list, it looks like we differ in the following

  1. devtools. I have 1.13.0, you 1.13.3
  2. dplyr, I’m running github version, you CRAN 0.7.4
  3. ggplot, same as dplyr github vs CRAN
  4. hms, 0.4.0 vs 0.3
  5. lazyeval 0.2.1 vs 0.2.0
  6. purrr, github vs CRAN
  7. tidyselect, I have it and you do not.
  8. withr, github vs CRAN
  9. yaml, I don’t have it, you do.

HTH,
Brandon

Session info ----------------------------------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.2 (2017-09-28)
 system   x86_64, darwin15.6.0        
 ui       AQUA                        
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/Los_Angeles         
 date     2017-12-13                  

Packages --------------------------------------------------------------------------------------------------------------------------------------------------------------
 package    * version    date       source                              
 assertthat   0.2.0      2017-04-11 CRAN (R 3.4.0)                      
 base       * 3.4.2      2017-10-04 local                               
 bindr        0.1        2016-11-13 cran (@0.1)                         
 bindrcpp   * 0.2        2017-06-17 cran (@0.2)                         
 broom        0.4.2      2017-02-13 cran (@0.4.2)                       
 cellranger   1.1.0      2016-07-27 cran (@1.1.0)                       
 cli          1.0.0      2017-11-05 cran (@1.0.0)                       
 colorspace   1.3-2      2016-12-14 CRAN (R 3.4.0)                      
 compiler     3.4.2      2017-10-04 local                               
 crayon       1.3.4      2017-10-31 Github (r-lib/crayon@b5221ab)       
 datasets   * 3.4.2      2017-10-04 local                               
 devtools     1.13.0     2017-05-08 CRAN (R 3.4.0)                      
 digest       0.6.12     2017-01-27 CRAN (R 3.4.0)                      
 dplyr      * 0.7.4.9000 2017-11-28 Github (tidyverse/dplyr@fc66342)    
 forcats    * 0.2.0      2017-01-23 cran (@0.2.0)                       
 foreign      0.8-69     2017-06-22 CRAN (R 3.4.2)                      
 ggplot2    * 2.2.1.9000 2017-12-08 Github (tidyverse/ggplot2@7b5c185)  
 glue         1.2.0      2017-10-29 cran (@1.2.0)                       
 graphics   * 3.4.2      2017-10-04 local                               
 grDevices  * 3.4.2      2017-10-04 local                               
 grid         3.4.2      2017-10-04 local                               
 gtable       0.2.0      2016-02-26 CRAN (R 3.4.0)                      
 haven        1.1.0      2017-07-09 cran (@1.1.0)                       
 hms          0.4.0      2017-11-23 cran (@0.4.0)                       
 httr         1.3.1      2017-08-20 cran (@1.3.1)                       
 jsonlite     1.5        2017-06-01 cran (@1.5)                         
 lattice      0.20-35    2017-03-25 CRAN (R 3.4.2)                      
 lazyeval     0.2.1      2017-10-29 cran (@0.2.1)                       
 lubridate    1.7.1      2017-11-03 cran (@1.7.1)                       
 magrittr     1.5        2014-11-22 CRAN (R 3.4.0)                      
 memoise      1.1.0      2017-04-21 CRAN (R 3.4.0)                      
 methods    * 3.4.2      2017-10-04 local                               
 mnormt       1.5-5      2016-10-15 cran (@1.5-5)                       
 modelr       0.1.1      2017-07-24 cran (@0.1.1)                       
 munsell      0.4.3      2016-02-13 CRAN (R 3.4.0)                      
 nlme         3.1-131    2017-02-06 CRAN (R 3.4.2)                      
 parallel     3.4.2      2017-10-04 local                               
 pkgconfig    2.0.1      2017-03-21 cran (@2.0.1)                       
 plyr         1.8.4      2016-06-08 CRAN (R 3.4.0)                      
 psych        1.7.3.21   2017-03-22 cran (@1.7.3.2)                     
 purrr      * 0.2.4.9000 2017-11-28 Github (tidyverse/purrr@62b135a)    
 R6           2.2.2      2017-06-17 cran (@2.2.2)                       
 Rcpp         0.12.14    2017-11-23 cran (@0.12.14)                     
 readr      * 1.1.1      2017-05-16 cran (@1.1.1)                       
 readxl       1.0.0      2017-04-18 cran (@1.0.0)                       
 reshape2     1.4.2      2016-10-22 CRAN (R 3.4.0)                      
 rlang        0.1.4.9000 2017-12-08 Github (tidyverse/rlang@5ebcf24)    
 rstudioapi   0.7        2017-09-07 cran (@0.7)                         
 rvest        0.3.2      2016-06-17 cran (@0.3.2)                       
 scales       0.5.0.9000 2017-10-10 Github (hadley/scales@d767915)      
 stats      * 3.4.2      2017-10-04 local                               
 stringi      1.1.5      2017-04-07 CRAN (R 3.4.0)                      
 stringr    * 1.2.0      2017-02-18 CRAN (R 3.4.0)                      
 tibble     * 1.3.4      2017-08-22 cran (@1.3.4)                       
 tidyr      * 0.7.2      2017-10-16 cran (@0.7.2)                       
 tidyselect   0.2.3      2017-11-06 cran (@0.2.3)                       
 tidyverse  * 1.2.1      2017-12-08 Github (tidyverse/tidyverse@3769ff2)
 tools        3.4.2      2017-10-04 local                               
 utils      * 3.4.2      2017-10-04 local                               
 withr        2.1.0.9000 2017-12-08 Github (jimhester/withr@fe81c00)    
 xml2         1.1.1      2017-01-24 CRAN (R 3.4.0)