Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-step define/add funcs #47

Open
yl3394 opened this issue Jul 23, 2019 · 1 comment
Open

Add multi-step define/add funcs #47

yl3394 opened this issue Jul 23, 2019 · 1 comment

Comments

@yl3394
Copy link
Collaborator

yl3394 commented Jul 23, 2019

Can we have more than one step data-manipulation functions? For example

data %>%   group_by(age_range, 
           d_imp_rate_level) %>%
  summarise(n_users_mean = round(mean(n_users))) %>% 
  ungroup() %>%
  group_by(age_range) %>%
  mutate(total_n_users_mean = sum(n_users_mean),
         pctg_users_mean = percent(n_users_mean/total_n_users_mean, accuracy = 0.01))

The second step computation is based on the the output of step 1.

@teramonagi
Copy link
Member

Using our example data, you can convert the following code

df <- dummy_data
library("dplyr")
df %>%
  group_by(gender, age) %>%
  summarise(impression = mean(impression)) %>% 
  ungroup() %>%
  group_by(gender) %>%
  mutate(total_impression = sum(impression), pctg_impression = impression/total_impression)

with

metrics_1 <- mmetrics::define(impression = mean(impression))
metrics_2 <- mmetrics::define(total_impression = sum(impression), pctg_impression = impression/total_impression)
df %>%
  mmetrics::add(gender, age, metrics = metrics_1) %>%
  mmetrics::add(gender, metrics = metrics_2, summarize=FALSE)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants