Ben Bolker
Ben Bolker

Reputation: 226087

"Variable context not set" error with mutate_at, dplyr version >= 0.7

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)

Upvotes: 25

Views: 4390

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226087

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 ...

Upvotes: 40

Related Questions