margins: Summarise all margins of a grouped data frame in one go

Description Usage Arguments Examples

View source: R/margins.R

Description

Take a data frame grouped with [dplyr::group_by()) and summarise by combinations of the grouping variables, e.g. by the first variable, by the first two, the first three, etc. With method = "combination" all combinations will be summarised, e.g. a, a and b, a and c, b and c. With method = "individual" each grouping variable will be used once, e.g. by the first variable, by the second variable, the third variable, etc.

The output is a data frame with NA in unused grouping variables. Alternatively use bind = FALSE to return a list of data frames, one per combination of grouping variables.

Usage

1
2
margins(.data, ..., bind = TRUE, method = c("hierarchy", "combination",
  "individual"), hierarchy = TRUE)

Arguments

.data

A tbl. All main verbs are S3 generics and provide methods for tbl_df(), dtplyr::tbl_dt() and dbplyr::tbl_dbi().

...

Name-value pairs of summary functions. The name will be the name of the variable in the result. The value should be an expression that returns a single value like min(x), n(), or sum(is.na(y)).

The arguments in ... are automatically quoted and evaluated in the context of the data frame. They support unquoting and splicing. See vignette("programming") for an introduction to these concepts.

bind

Whether to combine all margins into one data frame. Default is TRUE. If FALSE, returns a list of dataframes, one per margin.

method

One of "hierarchy", "combination", and "individual".

  • "hierarchy" will treat the grouping variables as a hierarchy with the first variable at the top and the next variable inside it, e.g. with three variables a, bandcthe combinations will beaalone,aandbtogether, anda, bandc' together.

  • "combination" summarises by all combinations of grouping variables, e.g. with three variables a, bandcthe combinations will beaalone,aandbtogether,aandctogether,bandctogether, anda, bandc' together.

  • "individual" summarises by one grouping variable at a time, e.g. with three variables a, bandcthe combinations will beaalone,balone, andc' alone.

hierarchy

Deprecated. The default TRUE is the same as method = "hierarchy". FALSE will do the same as method = "combination" and generate a message.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
mtcars %>%
  dplyr::group_by(cyl, gear, am) %>%
  margins(mpg = mean(mpg, na.rm = TRUE),
          hp = min(hp),
          bind = FALSE,
          method = "hierarchy")

mtcars %>%
  dplyr::group_by(cyl, gear, am) %>%
  margins(mpg = mean(mpg, na.rm = TRUE),
          hp = min(hp),
          bind = TRUE,
          method = "hierarchy") %>%
  print(n = Inf)

mtcars %>%
  dplyr::group_by(cyl, gear, am) %>%
  margins(mpg = mean(mpg, na.rm = TRUE),
          hp = min(hp),
          bind = FALSE,
          method = "combination")

mtcars %>%
  dplyr::group_by(cyl, gear, am) %>%
  margins(mpg = mean(mpg, na.rm = TRUE),
          hp = min(hp),
          bind = FALSE,
          method = "individual")

nacnudus/armgin documentation built on May 23, 2019, 6:07 p.m.