Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

dplyr

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)
like image 688
Ben Bolker Avatar asked Jun 25 '17 21:06

Ben Bolker


1 Answers

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

like image 192
Ben Bolker Avatar answered Nov 03 '22 10:11

Ben Bolker