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

Zero format #307

Closed
huanlusanofi opened this issue Mar 16, 2022 · 2 comments
Closed

Zero format #307

huanlusanofi opened this issue Mar 16, 2022 · 2 comments

Comments

@huanlusanofi
Copy link

Hi, is there any way re-format a completely empty total count 0 (0%) to be a single 0?

@gmbecker
Copy link
Contributor

(The second above failure is due to missing NEWS file entry, which has been fixed, check passed)

Hi @huanlusanofi

There are two ways you can do this:

The first and arguably "more correct conceptually" way is to specify a custom function as your format (which will require rtables >= 0.4.1.0004, as I found a bug when composing this response :( that I have now fixed)

formfun <- function(x) if(x[1] == 0) "0" else format_value(x, "xx (xx.x%)")

lyt <- basic_table() %>%
    split_cols_by("SEX", split_fun = keep_split_levels(c("F", "M"))) %>%
    split_rows_by("RACE", split_label = "Ethnicity", #5
                   label_pos = "topleft",
                  split_fun = keep_split_levels(c("ASIAN", "WHITE"))) %>%
    summarize_row_groups(format = formfun) %>%                       #4
    analyze("AGE", afun = mean, format = "xx.x")
> build_table(lyt, DM[1:15,])
Ethnicity       F           M    
—————————————————————————————————
ASIAN       6 (75.0%)   4 (57.1%)
  mean        29.3        34.8   
WHITE           0       1 (14.3%)
  mean         NA         31.0   

I will say that personally, I don't think the above is better, because that entry cannot be immediately distinguished from the means above and below it visually. I would recommend something like "0 ( - )" or similar if the "0 (0.0%)" is really a big problem, but the approach would be the same.

The second way you can do this, which is the work around for the currently released version of rtables, unfortunately, due to the bug I mentioned above, is to write your own content function which has that conditionality in it:

cfun <- function(df, labelstr, .N_col) {

    n <- NROW(df)
    if(n == 0)
        rc <- rcell(0, format = "xx")
    else
        rc <- rcell(c(n, n/.N_col), format = "xx (xx.x%)")
    in_rows(.names = labelstr, rc)
}


lyt2 <-  basic_table() %>%
    split_cols_by("SEX", split_fun = keep_split_levels(c("F", "M"))) %>%
    split_rows_by("RACE", split_label = "Ethnicity", #5
                   label_pos = "topleft",
                  split_fun = keep_split_levels(c("ASIAN", "WHITE"))) %>%
    summarize_row_groups(cfun = cfun) %>%                       #4
    analyze("AGE", afun = mean, format = "xx.x")
> build_table(lyt2, DM[1:15,])
Ethnicity       F           M    
—————————————————————————————————
ASIAN       6 (75.0%)   4 (57.1%)
  mean        29.3        34.8   
WHITE           0       1 (14.3%)
  mean         NA         31.0   

I don't think we're likely to add canned conditionality of the nature you describe to the formats specified by format strings, because of the combinatorial explosion that is likely to introduce in the number of format strings, so I will close this. Thank you for the question and please feel free to reach out again.

@huanlusanofi
Copy link
Author

Thank you very much @gmbecker , I'm truly appreciated.

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