此代码曾用于2017年5月1日左右(dplyr
版本0.5.0).对于dplyr
0.7版本,它失败了Error: Variable context not set
.我找不到解决方案谷歌搜索或查看dplyr NEWS文件.
这部分很好(设置示例 - 可能会简化......)
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")))
Run Code Online (Sandbox Code Playgroud)
现在尝试使用mutate_at()
:dplyr
版本> = 0.7.0 失败
hw1 %>% mutate_at(starts_with("HW"),repl_1_NA)
Run Code Online (Sandbox Code Playgroud)
Ben*_*ker 40
当starts_with()
用作列选择器时mutate_at
,我们现在需要将其包装起来vars()
,因此应该读取最终的代码行
hw1 %>% mutate_at(vars(starts_with("HW")),repl_1_NA)
Run Code Online (Sandbox Code Playgroud)
我通过查看这个问题的解决方案来解决这个问题,并认为我会将其作为其他人的路标发布在此...