小编cbo*_*cbo的帖子

dplyr ::如何使用函数中的变量引用进行变异?

有人可以告诉我如何使用dplyr将带有参数名称的向量传递给函数吗?

library("dplyr", quietly = TRUE, warn.conflicts = FALSE) # version 0.8.0.1

# Does not work
iris %>% rowwise() %>%  mutate(v1 = mean( as.name(names(iris)[-5]) ) )
iris %>% rowwise() %>%  mutate(v1 = mean( !!(names(iris)[-5]) ) )
iris %>% rowwise() %>%  mutate(v1 = mean( enquo(names(iris)[-5]) ) )
iris %>% rowwise() %>%  
mutate(v1 = mean( c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")  ) )

# This works and is the intended result
iris %>% rowwise() %>%  
mutate(v1 = mean( c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width )  ) ) …
Run Code Online (Sandbox Code Playgroud)

r dplyr

2
推荐指数
1
解决办法
54
查看次数

标签 统计

dplyr ×1

r ×1