25

This code used to work, as of about May 1 2017 (dplyr version 0.5.0). With dplyr version 0.7 it fails with Error: Variable context not set. I couldn't find the solution googling around or looking in the dplyr NEWS file.

This part is fine (setting up the example - could probably be simplified ...)

xx <- data.frame(stud_number=1:3,HW1=rep(0,3),HW2=c(NA,1,1),junk=rep(NA,3))
repl_1_NA <- function(x) { return(replace(x,which(x==1),NA)) }
hw1 <- xx %>% select(c(stud_number,starts_with("HW")))

Now try to use mutate_at(): fails with dplyr version >= 0.7.0

hw1 %>% mutate_at(starts_with("HW"),repl_1_NA)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453

1 Answers1

40

When using starts_with() as the column selector for mutate_at, we now need to wrap it in vars(), so the final code line should read

hw1 %>% mutate_at(vars(starts_with("HW")),repl_1_NA)

I figured this out by looking at the solution to this question and thought I would post it here as a signpost for others ...

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 1
    Am I correct that in a dplyr::select or dplyr::distinct statement using multiple calls to dplyr::starts_with, every single dplyr::starts_with must be wrapped in a dplyr::vars? I tried wrapping them all in dplyr::vars (as I would in dplyr::mutate_at) and it didn't work. If this is the case, I am NOT a fan of this change. – Brash Equilibrium Jul 11 '17 at 17:17