knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Are you boring of repeated tabulation work? This package is for you. With this package, you can reuse metrics which you define and easily do tabulation work on different analysis axes. Focus on more productive things with this package! Have a wonderful life!
First, we load dummy data from {mmetrics} package for this example.
# Load dummy data df <- mmetrics::dummy_data df
As a next step, we define metrics to evaluate using mmetrics::define
.
# Example metrics metrics <- mmetrics::define( cost = sum(cost), ctr = sum(click)/sum(impression) )
Call mmetrics::add()
with grouping key (here gender
) then we will get new data.frame
with defined metrics.
mmetrics::add(df, gender, metrics = metrics)
We can also use multiple grouping keys.
mmetrics::add(df, gender, age, metrics = metrics)
If we do not specify any grouping keys, mmetrics::add()
summarize all data as a default option.
mmetrics::add(df, metrics = metrics)
If we want mmetrics::add()
to behave like dplyr::mutate()
use mmetrics::disaggregate()
.
mmetrics::add(df, metrics = mmetrics::disaggregate(metrics), summarize = FALSE)
mmetrics::disaggregate()
It is hassle for users to re-define metrics when you would like to use these for dplyr::mutate()
.
In this case, you can use mmetrics::disaggregate()
to remove the first aggregation function for the argument and return disaggregated metrics.
# Original metrics. sum() is used for this metrics
metrics
# Disaggregate metrics! metrics_disaggregated <- mmetrics::disaggregate(metrics) # Woo! sum() are removed!!! metrics_disaggregated
You can use these metrics with dplyr::mutate()
for row-wise metrics computation.
dplyr::mutate(df, !!!metrics_disaggregated)
...or, you can do the same compucation using mmetrics::gmutate()
defind in our package.
In this case, you do not need to write !!!
(bang-bang-bang) operator explicitly.
mmetrics::gmutate(df, metrics = metrics_disaggregated)
mmetrics::add()
is a just wrapper function for mmetrics::gmutate()
and mmetrics::gsummarize()
.
We can use these functions directly instead of mmetrics::add()
.
# Completely the same result with mmetrics::add(df, gender, metrics = metrics) mmetrics::gsummarize(df, gender, metrics = metrics)
metrics::gmutate()
is useful to calculate the metrics like "ratio in a group".
# Cost ratio in each gender group mmetrics::gmutate(df, gender, metrics = mmetrics::define(cost_ratio = cost/sum(cost)))
If you would like to run code with multiple keys all at onece, you can use the combination of !!(bangbang operator)
and rlang::sym
as the following:
# Define keys keys <- c("gender", "age") # Run purrr::map(keys, ~ mmetrics::add(df, !!rlang::sym(.x), metrics = metrics))
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.