Error message when specifying "package::fun" in mutate_at

25 views
Skip to first unread message

Bob

unread,
Apr 10, 2017, 11:32:39 AM4/10/17
to manipulatr
Hi All,

I was comparing dplyr::recode to car::recode (allows recoding of ranges) when I discovered that specifying "package::fun" caused an error in mutate_at:

Restarting R session...

> library("tidyverse")
Loading tidyverse: ggplot2
Loading tidyverse: tibble
Loading tidyverse: tidyr
Loading tidyverse: readr
Loading tidyverse: purrr
Loading tidyverse: dplyr
Conflicts with tidy packages --------------------------------
filter(): dplyr, stats
lag():    dplyr, stats

> q1 <- 1:5
> q2 <- 1:5
> (mytbl <- tibble(q1, q2))
# A tibble: 5 × 2
     q1    q2
  <int> <int>
1     1     1
2     2     2
3     3     3
4     4     4
5     5     5

Here I'm not specifying which recode to use, but since only tidyverse is loaded, it'll use the one from dplyr, and it works fine:

> mutate_at(mytbl,
+           vars(starts_with("q")),
+           funs( recode(.,`1`=2L, `5`= 4L)))
# A tibble: 5 × 2
     q1    q2
  <int> <int>
1     2     2
2     2     2
3     3     3
4     4     4
5     4     4


Next, I specify "dplyr::recode" and I get an error message:

> mutate_at(mytbl,
+           vars(starts_with("q")),
+           funs(dplyr::recode(.,`1`=2L, `5`= 4L)))
Error in vapply(dots[missing_names], function(x) make_name(x$expr), character(1)) : 
  values must be length 1,
 but FUN(X[[1]]) result is length 3

Then I checked to see that "package::fun" works outside the mutate_at:

> dplyr::recode(q1,`1`=2L, `5`= 4L)
[1] 2 2 3 4 4

Is this a bug, or simply a limitation of how mutate_at works?

Thanks,
Bob


> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] dplyr_0.5.0     purrr_0.2.2     readr_1.1.0    
[4] tidyr_0.6.1     tibble_1.2      ggplot2_2.2.1  
[7] tidyverse_1.1.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.10     xml2_1.1.1       magrittr_1.5    
 [4] hms_0.3          rvest_0.3.2      mnormt_1.5-5    
 [7] munsell_0.4.3    colorspace_1.3-2 lattice_0.20-34 
[10] R6_2.2.0         httr_1.2.1       stringr_1.2.0   
[13] plyr_1.8.4       tools_3.3.3      parallel_3.3.3  
[16] grid_3.3.3       broom_0.4.2      nlme_3.1-131    
[19] gtable_0.2.0     psych_1.6.12     DBI_0.6         
[22] modelr_0.1.0     readxl_0.1.1     lazyeval_0.2.0  
[25] assertthat_0.1   reshape2_1.4.2   haven_1.0.0     
[28] stringi_1.1.2    forcats_0.2.0    scales_0.4.1    
[31] lubridate_1.6.0  jsonlite_1.3     foreign_0.8-67  

Hadley Wickham

unread,
Apr 10, 2017, 11:42:50 AM4/10/17
to Bob, manipulatr
Should be fixed in the dev version.
Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to manipulatr+...@googlegroups.com.
> To post to this group, send email to manip...@googlegroups.com.
> Visit this group at https://groups.google.com/group/manipulatr.
> For more options, visit https://groups.google.com/d/optout.



--
http://hadley.nz

Bob

unread,
Apr 10, 2017, 1:11:46 PM4/10/17
to manipulatr
Yep, it is. Thanks!
Reply all
Reply to author
Forward
0 new messages