该代码显示错误消息如下
This tidyselect interface doesn't support predicates.
这意味着lazy_dt
不支持across
?谢啦!
library(tidyverse)
library(dtplyr)
diamonds %>% lazy_dt() %>% group_by(color) %>%
summarise(across(where(is.numeric), ~ sum(.)))
该代码显示错误消息如下
This tidyselect interface doesn't support predicates.
这意味着lazy_dt
不支持across
?谢啦!
library(tidyverse)
library(dtplyr)
diamonds %>% lazy_dt() %>% group_by(color) %>%
summarise(across(where(is.numeric), ~ sum(.)))
问题是dtplyr
不支持where()
.参见GH上的this期封闭期.
相反,一种可能的解决方法是使用包含all_of
的数字列名称的向量:
library(tidyverse)
library(dtplyr)
num_cols <- names(diamonds)[
sapply(diamonds, is.numeric)
]
diamonds %>%
lazy_dt() %>%
group_by(color) %>%
summarise(across(all_of(num_cols), ~ sum(.)))
#> Source: local data table [7 x 8]
#> Call: `_DT1`[, .(carat = sum(carat), depth = sum(depth), table = sum(table),
#> price = sum(price), x = sum(x), y = sum(y), z = sum(z)),
#> keyby = .(color)]
#>
#> color carat depth table price x y z
#> <ord> <dbl> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 D 4457. 418005. 388916. 21476439 36701. 36728. 22648.
#> 2 E 6445. 604104. 563241. 30142944 53017. 53090. 32729.
#> 3 F 7028. 588690. 548031. 35542866 53578. 53621. 33058.
#> 4 G 8708. 697361. 646903. 45158240 64111. 64141. 39579.
#> 5 H 7572. 513493. 477628. 37257301 49686. 49698. 30691.
#> 6 I 5568. 335331. 312184 27608146 33740. 33740. 20850.
#> # ℹ 1 more row
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
或使用summarise_if
(但请注意,summarise_if
已被取代):
diamonds %>%
lazy_dt() %>%
group_by(color) %>%
summarise_if(~ is.numeric(.x), sum)
#> Source: local data table [7 x 8]
#> Call: `_DT3`[, .(carat = sum(carat), depth = sum(depth), table = sum(table),
#> price = sum(price), x = sum(x), y = sum(y), z = sum(z)),
#> keyby = .(color)]
#>
#> color carat depth table price x y z
#> <ord> <dbl> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 D 4457. 418005. 388916. 21476439 36701. 36728. 22648.
#> 2 E 6445. 604104. 563241. 30142944 53017. 53090. 32729.
#> 3 F 7028. 588690. 548031. 35542866 53578. 53621. 33058.
#> 4 G 8708. 697361. 646903. 45158240 64111. 64141. 39579.
#> 5 H 7572. 513493. 477628. 37257301 49686. 49698. 30691.
#> 6 I 5568. 335331. 312184 27608146 33740. 33740. 20850.
#> # ℹ 1 more row
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results