当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


R dtplyr group_by.dtplyr_step 分组和取消分组

这些是 dplyr 的 group_by()ungroup() 泛型的方法。根据 arrange 参数的值,分组会转换为 [.data.tablekeybyby 参数。

用法

# S3 method for dtplyr_step
group_by(.data, ..., .add = FALSE, arrange = TRUE)

# S3 method for dtplyr_step
ungroup(x, ...)

参数

.data

lazy_dt()

...

group_by() 中,用于分组的变量或计算。计算始终在未分组的数据帧上完成。要对分组数据执行计算,您需要在 group_by() 之前使用单独的 mutate() 步骤。 nest_by() 中不允许进行计算。在 ungroup() 中,要从分组中删除的变量。

.add, add

FALSE 时,默认情况下,group_by() 将覆盖现有组。要添加到现有组,请使用 .add = TRUE

该参数以前称为 add ,但这阻止了创建名为 add 的新分组变量,并且与我们的命名约定冲突。

arrange

如果是 TRUE ,会自动按组排列后续分组操作的输出。如果是 FALSE ,输出顺序将保持不变。在生成的 data.table 代码中,它在使用 keyby ( TRUE ) 和 by ( FALSE ) 参数之间切换。

x

tbl()

例子

library(dplyr, warn.conflicts = FALSE)
dt <- lazy_dt(mtcars)

# group_by() is usually translated to `keyby` so that the groups
# are ordered in the output
dt %>%
 group_by(cyl) %>%
 summarise(mpg = mean(mpg))
#> Source: local data table [3 x 2]
#> Call:   `_DT15`[, .(mpg = mean(mpg)), keyby = .(cyl)]
#> 
#>     cyl   mpg
#>   <dbl> <dbl>
#> 1     4  26.7
#> 2     6  19.7
#> 3     8  15.1
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

# use `arrange = FALSE` to instead use `by` so the original order
# or groups is preserved
dt %>%
 group_by(cyl, arrange = FALSE) %>%
 summarise(mpg = mean(mpg))
#> Source: local data table [3 x 2]
#> Call:   `_DT15`[, .(mpg = mean(mpg)), by = .(cyl)]
#> 
#>     cyl   mpg
#>   <dbl> <dbl>
#> 1     6  19.7
#> 2     4  26.7
#> 3     8  15.1
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
源代码:R/step-group.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Group and ungroup。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。