Learn R Programming

tidymargins

The goal of tidymargins is to provide the with_margins() adverb function, as well as the related spread_each() function.

Installation

You can install the released version of tidymargins from CRAN with:

install.packages("tidymargins")

Basic Usage

with_margins is very simple to use. It is an adverb, i.e. it accepts and returns a function. The function, passed in, we’ll call it original, should accept a table like data object as it’s first argument and return a similar object. The resulting function, we’ll call it modified, will also accept a data argument as it’s first argument however that argument is expected to be a grouped data frame. Other arguments to modified are passed along to the original function. When modified is called with a grouped data frame original is called for every possible subset of grouping variables, including the full set of all original grouping variables and the empty set implying no grouping, The results are bound together into a single data frame and returned.
The example below shows this as an example.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidymargins)
data(mtcars)

# our original function here is count
modified <- with_margins(count)

# modified is a function 
class(modified)
#> [1] "function"

modified(group_by(mtcars, Cylinders = cyl, Gears = gear))
CylindersGearsn
431
448
452
632
644
651
8312
852
4(All)11
6(All)7
8(All)14
(All)315
(All)412
(All)55
(All)(All)32

See that the output of modified is the counts grouped by; both cyl and gear, by cyl alone, by gear alone, and a grand total (no groups), all in a single data frame. When a variable is not used as a grouping variable it is replaced with the value "(All)", indicating that all levels of that variable are included. This label can be changes by specifying the all.name argument when calling with_margins, shown in the next example.

There is no need to store the intermediate function returned by with_margins, but since the returned function may also accept other arguments care should be taken to what is passing to which function. Care should also be taken with the pipe.

mtcars %>% 
    select(cyl, gear, mpg, disp, hp, qsec) %>% 
    group_by(cyl, gear) %>% 
    # <---------- with_margins ----------------><-- summarise --->
    with_margins(summarise_if, all.name='Total')(is.numeric, mean) %>% 
    # making pretty
    rename( "Miles/(US) gallon" = "mpg"
          , "Cylinders" = "cyl"
          , "Displacement (cu.in.)" = "disp"
          , "Gross horsepower" = "hp"
          , "1/4 mile time" = "qsec"
          , "Number of forward gears" = "gear"
          )
CylindersNumber of forward gearsMiles/(US) gallonDisplacement (cu.in.)Gross horsepower1/4 mile time
4321.50000120.100097.0000020.01000
4426.92500102.625076.0000019.61250
4528.20000107.7000102.0000016.80000
6319.75000241.5000107.5000019.83000
6419.75000163.8000116.5000017.67000
6519.70000145.0000175.0000015.50000
8315.05000357.6167194.1666717.14250
8515.40000326.0000299.5000014.55000
4Total26.66364105.136482.6363619.13727
6Total19.74286183.3143122.2857117.97714
8Total15.10000353.1000209.2142916.77214
Total316.10667326.3000176.1333317.69200
Total424.53333123.016789.5000018.96500
Total521.38000202.4800195.6000015.64000
TotalTotal20.09062230.7219146.6875017.84875

Copy Link

Version

Install

install.packages('tidymargins')

Monthly Downloads

4

Version

0.1.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Andrew Redd

Last Published

July 23rd, 2019

Functions in tidymargins (0.1.0)

spread_each

Spread multiple variables
with_margins

Operate over margins