该代码显示错误消息如下

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

R相关问答推荐

使用sensemakr和fixest feols模型(R)

MCMC和零事件二元逻辑回归

如何编辑ggplot的图例字使用自定义对象(gtable)?'

修改用R编写的用户定义函数

如何在一次运行中使用count进行多列计数

当我们有多个特殊字符时,使用gsub删除名称和代码'

如何计算多个日期是否在一个日期范围内

如果可能,将数字列转换为整数,否则保留为数字

将饼图插入条形图

在R中使用Scale_y_Break后更改y轴标签

可以替代与NSE一起使用的‘any_of()’吗?

线性模型斜率在减少原始数据时提供NA

在GG图中绘制射线的自动程序

如果COLSUM为>;0,则COLNAME为向量

如何在R中使用hmm TMB提前一步预测观察到的状态?

将数据集旋转到长格式,用于遵循特定名称模式的所有变量对

如何提取R中其他字符串和数字之间的字符串?

数据集上的R循环和存储模型系数

将数据从一列转换为按组累计计数的单个虚拟变量

使用LAG和dplyr执行计算,以便按行和按组迭代