Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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
模拟R函数中的次参数_R_Tidyverse_Rlang_Quosure - Fatal编程技术网

模拟R函数中的次参数

模拟R函数中的次参数,r,tidyverse,rlang,quosure,R,Tidyverse,Rlang,Quosure,我希望创建一个函数,该函数接受(数据帧)变量列表作为其参数之一。我设法使它部分工作,但当我按/计数进入小组时,情况就不一样了。我该怎么做 ## Works f1 <- function(dfr, ..., split = NULL) { dots <- rlang::enquos(...) split <- rlang::enquos(split) dfr %>% select(!!!dots, !!!split) %>% gather(

我希望创建一个函数,该函数接受(数据帧)变量列表作为其参数之一。我设法使它部分工作,但当我按/计数进入小组时,情况就不一样了。我该怎么做

## Works
f1 <- function(dfr, ..., split = NULL) {
  dots <- rlang::enquos(...)
  split <- rlang::enquos(split)
  dfr %>%
    select(!!!dots, !!!split) %>%
    gather('type', 'score', -c(!!!split))
}

## does not work
f2 <- function(dfr, ..., split = NULL) {
  dots <- rlang::enquos(...)
  split <- rlang::enquos(split)
  dfr %>%
    select(!!!dots, !!!split) %>%
    gather('type', 'score', -c(!!!split))
    count(!!!split, type, score)
  }

使用
f1()
进行的这些调用都可以工作,但对于
f2
则没有任何命令可以工作。它们最终都会出现
错误!拆分:参数类型无效
。如果没有
split
参数,
f2(drat:qsec)
就不能(立即)工作,我对此并不感到惊讶,但是如何让第二个和第三个注释工作呢?

第二个函数的问题(尽管缺少管道)是
count()
(或者更确切地说
groupby()
count()
)调用的
不支持tidyselect语法,因此您不能像使用
select()
gather()
等一样向它传递要拼接的列表。相反,一个选项是使用
group\u by\u at()
add\u tally()
。下面是该函数的一个稍加修改的版本:

library(dplyr)

f2 <- function(dfr, ..., split = NULL) {
  dfr %>%
    select(..., {{split}}) %>%
    gather('type', 'score', -{{split}}) %>%
    group_by_at(vars({{split}}, type, score)) %>% # could use `group_by_all()`
    add_tally()
}

mtcars %>% f2(drat:qsec)

# A tibble: 96 x 3
# Groups:   type, score [81]
   type  score     n
   <chr> <dbl> <int>
 1 drat   3.9      2
 2 drat   3.9      2
 3 drat   3.85     1
 4 drat   3.08     2
 5 drat   3.15     2
 6 drat   2.76     2
 7 drat   3.21     1
 8 drat   3.69     1
 9 drat   3.92     3
10 drat   3.92     3
# ... with 86 more rows

mtcars %>% f2(drat:qsec, split = c(gear, carb))

# A tibble: 96 x 5
# Groups:   gear, carb, type, score [89]
    gear  carb type  score     n
   <dbl> <dbl> <chr> <dbl> <int>
 1     4     4 drat   3.9      2
 2     4     4 drat   3.9      2
 3     4     1 drat   3.85     1
 4     3     1 drat   3.08     1
 5     3     2 drat   3.15     2
 6     3     1 drat   2.76     1
 7     3     4 drat   3.21     1
 8     4     2 drat   3.69     1
 9     4     2 drat   3.92     1
10     4     4 drat   3.92     2
# ... with 86 more rows
库(dplyr)
f2%
选择(…,{{split}})%>%
聚集('type','score',-{{split}})%>%
group_by_at(vars({{split},type,score))%>%#可以使用'group_by_all()`
加上()
}
mtcars%>%f2(图纸:qsec)
#一个tibble:96x3
#分组:类型、分数[81]
类型分数n
1德拉特3.9 2
2德拉特3.9 2
3德拉特3.85 1
4德拉特3.08 2
5德拉特3.15 2
6德拉特2.76 2
7德拉特3.21 1
8德拉特3.69 1
9德拉特3.92 3
10德拉特3.92 3
# ... 还有86行
mtcars%>%f2(drat:qsec,分割=c(齿轮,carb))
#一个tibble:96x5
#分组:装备、碳水化合物、类型、分数[89]
齿轮carb型刻痕n
1 4 drat 3.9 2
2 4 drat 3.9 2
3.4.1德拉特3.85 1
4 3 1 drat 3.08 1
5 3 2德拉特3.15 2
6 3 1德拉特2.76 1
7 3 4 drat 3.21 1
8 4 2 drat 3.69 1
9 4 2 drat 3.92 1
10 4 drat 3.92 2
# ... 还有86行

谢谢,很有价值的例子@H 1,我花了比预期更多的精力将其翻译成我的真实例子,但最终成功了。
library(dplyr)

f2 <- function(dfr, ..., split = NULL) {
  dfr %>%
    select(..., {{split}}) %>%
    gather('type', 'score', -{{split}}) %>%
    group_by_at(vars({{split}}, type, score)) %>% # could use `group_by_all()`
    add_tally()
}

mtcars %>% f2(drat:qsec)

# A tibble: 96 x 3
# Groups:   type, score [81]
   type  score     n
   <chr> <dbl> <int>
 1 drat   3.9      2
 2 drat   3.9      2
 3 drat   3.85     1
 4 drat   3.08     2
 5 drat   3.15     2
 6 drat   2.76     2
 7 drat   3.21     1
 8 drat   3.69     1
 9 drat   3.92     3
10 drat   3.92     3
# ... with 86 more rows

mtcars %>% f2(drat:qsec, split = c(gear, carb))

# A tibble: 96 x 5
# Groups:   gear, carb, type, score [89]
    gear  carb type  score     n
   <dbl> <dbl> <chr> <dbl> <int>
 1     4     4 drat   3.9      2
 2     4     4 drat   3.9      2
 3     4     1 drat   3.85     1
 4     3     1 drat   3.08     1
 5     3     2 drat   3.15     2
 6     3     1 drat   2.76     1
 7     3     4 drat   3.21     1
 8     4     2 drat   3.69     1
 9     4     2 drat   3.92     1
10     4     4 drat   3.92     2
# ... with 86 more rows