如何使用dplyr在UDF中添加不确定数量的字符串参数

时间:2016-05-06 21:40:48

标签: r dplyr

我想通过使用dplyr将一个字符串或多个字符串传递给函数,但不知何故,它只接受参数中的第一个变量而忽略其他变量

library(lazyeval)
plotGenerationFct = function(data,..., targetVariable){   
  result = data %>% select_(..., targetVariable) %>% group_by_(...) %>% summarise_(mean= interp(~mean(var, na.rm = TRUE), var = as.name(targetVariable)))
  return(result)
}

以下表达式给出了相同的结果

plotGenerationFct(diamonds, c("cut"), targetVariable = "price")
plotGenerationFct(diamonds, c("cut","color"), targetVariable = "price")
plotGenerationFct(diamonds, c("cut","color","clarity"), targetVariable = "price")

1 个答案:

答案 0 :(得分:1)

dplyr函数的标准评估版本是净设置为接受矢量作为标准参数。为此使用.dots=参数

plotGenerationFct = function(data, vars, targetVariable){   
  result = data %>% select_(.dots=c(vars, targetVariable)) %>% 
      group_by_(.dots=vars) %>%  
      summarise_(mean= interp(~mean(var, na.rm = TRUE), var = as.name(targetVariable)))
  return(result)
}

所以这些都是一样的

select(diamonds, cut, color)
select_(diamonds, "cut", "color")
select_(diamonds, .dots=c("cut", "color"))