[英]Passing argument from custom function to group_by doesn't work
我不知道為什么將自定義 function 的參數傳遞給group_by
不起作用。 我只是從數據集中傳遞一個colName
,當我運行我自己的 function 時出現錯誤:必須按在.data 中找到的變量分組。 未找到列“colName”。 在下面的示例中,我使用quakes
環境中可用的 quakes 數據集:
foo <- function(data, colName) {
result <- data %>%
group_by(colName) %>%
summarise(count = n())
return(result)
}
foo(quakes, "stations")
# I also tried passing w/o commas but it is not working too:
# foo(quakes, stations)
我注意到,當我將列名顯式傳遞給group_by
時,它可以工作:
group_by(stations) %>%
但是,在 function 中硬編碼列名是沒有意義的。
我相信您只需將變量名稱包裝在get
中。
foo <- function(data, colName) {
result <- data %>%
dplyr::group_by(get(colName)) %>%
dplyr::summarise(count = n())
return(result)
}
> foo(quakes, "stations")
# A tibble: 102 x 2
`get(colName)` count
<int> <int>
1 10 20
2 11 28
3 12 25
4 13 21
5 14 39
6 15 34
7 16 35
8 17 38
9 18 33
10 19 29
這是使它工作的另一種方法。 您可以將.data[[var]]
構造用於存儲為字符串的列名:
foo <- function(data, colName) {
result <- data %>%
group_by(.data[[colName]]) %>%
summarise(count = n())
return(result)
}
foo(quakes, "stations")
# A tibble: 102 x 2
stations count
<int> <int>
1 10 20
2 11 28
3 12 25
4 13 21
5 14 39
6 15 34
7 16 35
8 17 38
9 18 33
10 19 29
# ... with 92 more rows
如果您決定不將ColName
作為字符串傳遞,您可以在 function 內用一對花括號將其包裹起來,以獲得類似的結果:
foo <- function(data, colName) {
result <- data %>%
group_by({{ colName }}) %>%
summarise(count = n())
return(result)
}
foo(quakes, stations)
# A tibble: 102 x 2
stations count
<int> <int>
1 10 20
2 11 28
3 12 25
4 13 21
5 14 39
6 15 34
7 16 35
8 17 38
9 18 33
10 19 29
# ... with 92 more rows
使用 dplyr 嘗試:
library(dplyr)
foo <- function(data, colName) {
colName = sym(colName)
result <- data %>%
group_by(!!colName) %>%
summarise(count = n())
return(result)
}
foo(quakes, "stations")
#> # A tibble: 102 x 2
#> stations count
#> <int> <int>
#> 1 10 20
#> 2 11 28
#> 3 12 25
#> 4 13 21
#> 5 14 39
#> 6 15 34
#> 7 16 35
#> 8 17 38
#> 9 18 33
#> 10 19 29
#> # ... with 92 more rows
由代表 package (v2.0.0) 於 2021 年 5 月 4 日創建
一個選項也是使用ensym
和評估 ( !!
) 以便它可以接受帶引號和不帶引號的參數
foo <- function(data, colName) {
data %>%
dplyr::group_by(!! rlang::ensym(colName)) %>%
dplyr::summarise(count = n())
}
foo(quakes, stations)
foo(quakes, "stations")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.