group_dt



    描述

    在指定的组内执行数据操作。

    Usage

    group_dt(.data, by = NULL, …)

    rowwise_dt(.data, …)

    Arguments

    .data A data.frame
    by 分组变量的无引号名称列表中的分组变量的无引号名称。
    可以在data.frame上实现的任何数据操作参数。

    detail

    如果您想在group_dt中使用summarise_dt和mutate_dt,那么最好在这些函数中使用“by”参数,这样会快得多,因为您不需要使用. sd(这会花费额外的时间来复制)。

    1. iris %>% group_dt(by = Species,slice_dt(1:2)) #equal to
    2. iris %>% group_by_dt(Species) %>% group_exe_dt(head(2))
    3. iris %>% group_dt(Species,filter_dt(Sepal.Length == max(Sepal.Length)))
    4. iris %>% group_dt(Species,summarise_dt(new = max(Sepal.Length)))
    5. # you can pipe in the `group_dt`
    6. iris %>% group_dt(Species,
    7. mutate_dt(max= max(Sepal.Length)) %>%
    8. summarise_dt(sum=sum(Sepal.Length)))
    9. # for users familiar with data.table, you can work on .SD directly
    10. # following codes get the first and last row from each group
    11. iris %>%
    12. group_dt(
    13. by = Species,
    14. rbind(.SD[1],.SD[.N])
    15. )
    16. #' # for summarise_dt, you can use "by" to calculate within the group
    17. mtcars %>%
    18. summarise_dt(
    19. disp = mean(disp),
    20. hp = mean(hp),
    21. by = cyl
    22. )
    23. # equal to
    24. mtcars %>%
    25. group_dt(cyl,
    26. summarise_dt(
    27. disp = mean(disp),
    28. hp = mean(hp))
    29. )
    30. mtcars %>%
    31. group_dt(by =.(vs,am),
    32. summarise_dt(avg = mean(mpg)))
    33. mtcars %>%
    34. group_dt(by =.(vs,am),
    35. summarise_dt(avg = mean(mpg)))
    36. # examples for `rowwise_dt`
    37. df <- data.table(x = 1:2, y = 3:4, z = 4:5)
    38. df %>% mutate_dt(m = mean(c(x, y, z)))
    39. df %>% rowwise_dt(
    40. mutate_dt(m = mean(c(x, y, z)))
    41. )