【发布时间】:2021-08-16 00:04:57
【问题描述】:
我想在将使用dplyr::distinct() 的函数中指定未知数量的列名。我目前的尝试是:
myFunction <- function(table, id) {
table %>%
dplyr::distinct(.data[[id]])
}
我正在尝试上述 [.data[[id]]] 因为the data-masking section of this dplyr blog 状态:
当您有一个字符向量的 env 变量时,您需要使用 [[ 来索引 .data 代词,例如 summarise(df, mean = mean(.data[[var]]))。
dplyr::distinct() 的文档说明了它的第二个参数:
<data-masking>确定唯一性时使用的可选变量。如果给定的输入组合有多行,则仅保留第一行。如果省略,将使用所有变量。
火花
更具体地说,我正在尝试将此功能与 Spark 一起使用。
sc <- sparklyr::spark_connect(local = "master")
mtcars_tbl <- sparklyr::copy_to(sc, mtcars, "mtcars_spark")
##### desired return
mtcars_tbl %>% dplyr::distinct(cyl, gear)
# Source: spark<?> [?? x 2]
cyl gear
<dbl> <dbl>
1 6 4
2 4 4
3 6 3
4 8 3
5 4 3
6 4 5
7 8 5
8 6 5
##### myFunction fails
id = c("cyl", "gear")
myFunction(mtcars_tbl, id)
Error: Can't convert a call to a string
Run `rlang::last_error()` to see where the error occurred.
在this comment之后,我还有其他失败的尝试:
myFunction <- function(table, id) {
table %>%
dplyr::distinct(.dots = id)
}
myFunction(mtcars_tbl, id)
# Source: spark<?> [?? x 1]
.dots
<list>
1 <named list [2]>
#####
myFunction <- function(table, id) {
table %>%
dplyr::distinct_(id)
}
myFunction(mtcars_tbl, id)
Error in UseMethod("distinct_") :
no applicable method for 'distinct_' applied to an object of class "c('tbl_spark', 'tbl_sql', 'tbl_lazy', 'tbl')"
【问题讨论】:
标签: r apache-spark dplyr nse