用dplyr编程的变异公式错误

用dplyr编程的变异公式错误,r,dplyr,R,Dplyr,我遇到了一个问题,我正试图按照以下思路做一些事情: df <- tibble(x=1:5, y = c(1,0,1,0,1)) zup <- function(df, zp.col){ dz <- df %>% mutate_(z = ~x * !!zp.col) } foo <- zup(df, zp.col = "y") 任何帮助都将不胜感激 编辑 由于mutate\uuu已被替换,我更改为: zup <- function(df, zp.c

我遇到了一个问题,我正试图按照以下思路做一些事情:

df <- tibble(x=1:5, y = c(1,0,1,0,1))
zup <- function(df, zp.col){
  dz <- df %>%
  mutate_(z = ~x * !!zp.col)
}

foo <- zup(df, zp.col =  "y")
任何帮助都将不胜感激

编辑

由于
mutate\uuu
已被替换,我更改为:

zup <- function(df, zp.col){
  dz <- df %>%
    mutate(z = ~x + !!zp.col)
}

foo <- zup(df, zp.col =  "y")
即使更改为以下内容:

zup <- function(df, zp.col){
  out.col <- "z"
  dz <- df %>%
    mutate(!!out.col := ~x + !!zp.col)
}

foo <- zup(df, zp.col =  "y")

zup我想你的意思是在表达式中使用
enquo
,而不是
quo
。这也允许裸列名作为参数进入函数

library(dplyr)

df <- tibble(x = 1:5, y = c(1, 0, 1, 0, 1))

zup <- function(df, zp.col) {

  quo.col <- enquo(zp.col)

  df %>% mutate(z = x * !!quo.col)

}

zup(df, y)

# # A tibble: 5 x 3
#       x     y     z
#   <int> <dbl> <dbl>
# 1     1     1     1
# 2     2     0     0
# 3     3     1     3
# 4     4     0     0
# 5     5     1     5
库(dplyr)

如果您正在阅读的文档与较旧版本的
dplyr
相关,则不推荐使用下划线动词,如
mutate\uu
。有关使用
dplyr
@KevinArseneau进行非标准评估的较新方法,请参阅。感谢您的评论,据我所知,文档的实际文本是相同的,但我已相应地编辑了代码和问题。
zup <- function(df, zp.col){
  out.col <- "z"
  dz <- df %>%
    mutate(!!out.col := ~x + !!zp.col)
}

foo <- zup(df, zp.col =  "y")
library(dplyr)

df <- tibble(x = 1:5, y = c(1, 0, 1, 0, 1))

zup <- function(df, zp.col) {

  quo.col <- enquo(zp.col)

  df %>% mutate(z = x * !!quo.col)

}

zup(df, y)

# # A tibble: 5 x 3
#       x     y     z
#   <int> <dbl> <dbl>
# 1     1     1     1
# 2     2     0     0
# 3     3     1     3
# 4     4     0     0
# 5     5     1     5