Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数中具有列名称的dplyr_R_Function_Dplyr - Fatal编程技术网

函数中具有列名称的dplyr

函数中具有列名称的dplyr,r,function,dplyr,R,Function,Dplyr,无法理解如何使用dplyrR包在函数中使用列名。可复制的示例如下所示: 数据 set.seed(12345) Y <- rnorm(10) Env <- paste0("E", rep(1:2, each = 5)) Gen <- paste0("G", rep(1:5, times = 2)) df1 <- data.frame(Y, Env, Gen) 第一个功能 library(dplyr) df1 %>% dplyr::group_by(E,

无法理解如何使用
dplyr
R
包在函数中使用列名。可复制的示例如下所示:

数据

set.seed(12345)
Y <- rnorm(10)
Env <- paste0("E", rep(1:2, each = 5))
Gen <- paste0("G", rep(1:5, times = 2))
df1 <- data.frame(Y, Env, Gen)
第一个功能

library(dplyr)
  df1 %>%
    dplyr::group_by(E, G) %>%
    dplyr::summarize(mean(Y))

with(data = df1, expr = tapply(X = Y, INDEX = list(E, G), FUN = mean))  
fn1 <- function(Y, E, G, data){
  Y <- deparse(substitute(Y))
  E <- deparse(substitute(E))
  G <- deparse(substitute(G))
  Out <- with(data = data, tapply(X = Y, INDEX = list(E, G), FUN = mean), parent.frame())
  return(Out)
}  

fn1(Y = Y, E = Env, G = Gen, data = df1)
fn2 <- function(Y, E, G, data){
  Y <- deparse(substitute(Y))
  E <- deparse(substitute(E))
  G <- deparse(substitute(G))
  library(dplyr)
  Out <- df1 %>%
    dplyr::group_by(E, G) %>%
    dplyr::summarize(mean(Y))
  return(Out)
}  

fn2(Y = Y, E = Env, G = Gen, data = df1)

fn1一个选项是使用
enquo
捕获
quosure
对象中的表达式及其环境,该对象可在
组内通过
进行评估,
摘要
变异
等使用
运算符或
UQ
(不引用表达式)

不使用
rlang
的OP函数的另一个变体是使用
groupby\u at
summary\u at
将字符串作为参数

fn3 <- function(Y, E, G, data){
  Y <- deparse(substitute(Y))
  E <- deparse(substitute(E))
  G <- deparse(substitute(G))

   df1 %>%
    dplyr::group_by_at(vars(E, G)) %>%
    dplyr::summarize_at(vars(Y), mean)

}  

fn3(Y = Y, E = Env, G = Gen, data = df1)

fn3从您的数据中,什么是
E
G
,如果您能在我的代码中解释您的答案和问题,我们将不胜感激。Thanks@MYaseen208我添加了几个其他选项和一些解释
fn2 <- function(Y, E, G, data){
   Y <- deparse(substitute(Y))
   E <- deparse(substitute(E))
   G <- deparse(substitute(G))

   df1 %>%
    dplyr::group_by(!!rlang::sym(E), !! rlang::sym(G)) %>%
    dplyr::summarize(Y = mean(!! rlang::sym(Y)))

}  

fn2(Y = Y, E = Env, G = Gen, data = df1)
fn3 <- function(Y, E, G, data){
  Y <- deparse(substitute(Y))
  E <- deparse(substitute(E))
  G <- deparse(substitute(G))

   df1 %>%
    dplyr::group_by_at(vars(E, G)) %>%
    dplyr::summarize_at(vars(Y), mean)

}  

fn3(Y = Y, E = Env, G = Gen, data = df1)