我试图使用mutate将一列的名称赋值给一个变量。
df <-data.frame(x = sample(1:100, 50), y = rnorm(50))
new <- function(name){
df%>%mutate(name = ifelse(x <50, "small", "big"))
}
当我运行时,
new(name = "newVar")
这段代码无法正常工作。我知道mutate_()
可以帮助解决问题,但是我在将其与ifelse
一起使用时遇到了困难。
如果有任何帮助,将不胜感激。
mutate
结尾处缺少一个)
,而且mutate
拼写错误。此外,在函数中调用全局变量df
时要小心。最好将df
作为参数传递给函数。 - juanmutate_
is getting phased out. Updatedplyr
and typevignette('programming', 'dplyr')
- Axeman