Tplyr icon indicating copy to clipboard operation
Tplyr copied to clipboard

Add set_numeric_where functionality

Open elimillera opened this issue 3 years ago • 3 comments
trafficstars

set_numeric_where

This new function adds a filter step after the numeric_data is created to filter out rows that don't meet the cutoff. Columns, stat, and cutoffs can all be set

Examples

mtcars %>%
  tplyr_table(gear) %>%
  add_layer(
    group_count(cyl) %>%
      set_numeric_where(10, "n") %>%
      add_total_row() %>%
      set_order_count_method("bycount")
  ) %>% build()
# A tibble: 2 × 6
  row_label1 var1_3      var1_4        var1_5        ord_layer_index ord_layer_1
  <chr>      <chr>       <chr>         <chr>                   <int>       <dbl>
1 8          12 ( 80.0%) " 0 (  0.0%)" " 2 ( 40.0%)"               1           0
2 Total      15 (100.0%) "12 (100.0%)" " 5 (100.0%)"               1          12
load(test_path("adae.Rdata"))

adae %>%
  tplyr_table(TRTA) %>%
  add_layer(
    group_count(vars(AEBODSYS, AEDECOD)) %>%
      set_numeric_where(3, "n", "Placebo") %>%
      add_total_row()
  ) %>%
  build()
# A tibble: 13 × 8
   row_label1                                           row_label2    var1_Placebo `var1_Xanomeli…` `var1_Xanomeli…` ord_layer_index ord_layer_1 ord_layer_2
   <chr>                                                <chr>         <chr>        <chr>            <chr>                      <int>       <dbl>       <dbl>
 1 CARDIAC DISORDERS                                    "CARDIAC DIS… " 5 ( 10.6%… " 6 (  7.8%)"    " 6 (  7.9%)"                  1           1         Inf
 2 GASTROINTESTINAL DISORDERS                           "GASTROINTES… " 6 ( 12.8%… " 6 (  7.8%)"    " 3 (  3.9%)"                  1           2         Inf
 3 GASTROINTESTINAL DISORDERS                           "   DIARRHOE… " 3 (  6.4%… " 1 (  1.3%)"    " 2 (  2.6%)"                  1           2           1
 4 GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS "GENERAL DIS… "11 ( 23.4%… "21 ( 27.3%)"    "21 ( 27.6%)"                  1           3         Inf
 5 GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS "   APPLICAT… " 4 (  8.5%… " 7 (  9.1%)"    " 5 (  6.6%)"                  1           3           1
 6 INFECTIONS AND INFESTATIONS                          "INFECTIONS … " 5 ( 10.6%… " 4 (  5.2%)"    " 3 (  3.9%)"                  1           4         Inf
 7 INFECTIONS AND INFESTATIONS                          "   UPPER RE… " 4 (  8.5%… " 1 (  1.3%)"    " 1 (  1.3%)"                  1           4           1
 8 INVESTIGATIONS                                       "INVESTIGATI… " 3 (  6.4%… " 1 (  1.3%)"    " 1 (  1.3%)"                  1           5         Inf
 9 PSYCHIATRIC DISORDERS                                "PSYCHIATRIC… " 3 (  6.4%… " 2 (  2.6%)"    " 3 (  3.9%)"                  1           6         Inf
10 SKIN AND SUBCUTANEOUS TISSUE DISORDERS               "SKIN AND SU… " 7 ( 14.9%… "21 ( 27.3%)"    "26 ( 34.2%)"                  1           7         Inf
11 SKIN AND SUBCUTANEOUS TISSUE DISORDERS               "   ERYTHEMA" " 4 (  8.5%… " 3 (  3.9%)"    " 2 (  2.6%)"                  1           7           1
12 SKIN AND SUBCUTANEOUS TISSUE DISORDERS               "   PRURITUS" " 3 (  6.4%… " 8 ( 10.4%)"    " 7 (  9.2%)"                  1           7           2
13 Total                                                "Total"       "47 (100.0%… "77 (100.0%)"    "76 (100.0%)"                  1           8         Inf

elimillera avatar Feb 26 '22 18:02 elimillera

@elimillera looking at this output, note row 1:

# A tibble: 13 × 8
   row_label1                                           row_label2    var1_Placebo `var1_Xanomeli…` `var1_Xanomeli…` ord_layer_index ord_layer_1 ord_layer_2
   <chr>                                                <chr>         <chr>        <chr>            <chr>                      <int>       <dbl>       <dbl>
 1 CARDIAC DISORDERS                                    "CARDIAC DIS… " 5 ( 10.6%… " 6 (  7.8%)"    " 6 (  7.9%)"                  1           1         Inf
 2 GASTROINTESTINAL DISORDERS                           "GASTROINTES… " 6 ( 12.8%… " 6 (  7.8%)"    " 3 (  3.9%)"                  1           2         Inf
 3 GASTROINTESTINAL DISORDERS                           "   DIARRHOE… " 3 (  6.4%… " 1 (  1.3%)"    " 2 (  2.6%)"                  1           2           1
 4 GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS "GENERAL DIS… "11 ( 23.4%… "21 ( 27.3%)"    "21 ( 27.6%)"                  1           3         Inf
 5 GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS "   APPLICAT… " 4 (  8.5%… " 7 (  9.1%)"    " 5 (  6.6%)"                  1           3           1
 6 INFECTIONS AND INFESTATIONS                          "INFECTIONS … " 5 ( 10.6%… " 4 (  5.2%)"    " 3 (  3.9%)"                  1           4         Inf
 7 INFECTIONS AND INFESTATIONS                          "   UPPER RE… " 4 (  8.5%… " 1 (  1.3%)"    " 1 (  1.3%)"                  1           4           1
 8 INVESTIGATIONS                                       "INVESTIGATI… " 3 (  6.4%… " 1 (  1.3%)"    " 1 (  1.3%)"                  1           5         Inf
 9 PSYCHIATRIC DISORDERS                                "PSYCHIATRIC… " 3 (  6.4%… " 2 (  2.6%)"    " 3 (  3.9%)"                  1           6         Inf
10 SKIN AND SUBCUTANEOUS TISSUE DISORDERS               "SKIN AND SU… " 7 ( 14.9%… "21 ( 27.3%)"    "26 ( 34.2%)"                  1           7         Inf
11 SKIN AND SUBCUTANEOUS TISSUE DISORDERS               "   ERYTHEMA" " 4 (  8.5%… " 3 (  3.9%)"    " 2 (  2.6%)"                  1           7           1
12 SKIN AND SUBCUTANEOUS TISSUE DISORDERS               "   PRURITUS" " 3 (  6.4%… " 8 ( 10.4%)"    " 7 (  9.2%)"                  1           7           2
13 Total                                                "Total"       "47 (100.0%… "77 (100.0%)"    "76 (100.0%)"                  1           8         Inf

If there aren't any inner layer values, the outer layer should be suppressed.

mstackhouse avatar Feb 28 '22 13:02 mstackhouse

Hey @mstackhouse

That is updated. Here is the new functionality:

load(test_path("adae.Rdata"))

tmo <- adae %>%
  tplyr_table(TRTA) %>%
  add_layer(
    group_count(vars(AEBODSYS, AEDECOD)) %>%
      set_numeric_where(3, "n", "Placebo") %>%
      add_total_row()
  ) %>%
  build()

# A tibble: 10 × 8
   row_label1          row_label2 var1_Placebo `var1_Xanomeli…` `var1_Xanomeli…` ord_layer_index ord_layer_1
   <chr>               <chr>      <chr>        <chr>            <chr>                      <int>       <dbl>
 1 GASTROINTESTINAL D… "GASTROIN… " 6 ( 12.8%… " 6 (  7.8%)"    " 3 (  3.9%)"                  1           1
 2 GASTROINTESTINAL D… "   DIARR… " 3 (  6.4%… " 1 (  1.3%)"    " 2 (  2.6%)"                  1           1
 3 GENERAL DISORDERS … "GENERAL … "11 ( 23.4%… "21 ( 27.3%)"    "21 ( 27.6%)"                  1           2
 4 GENERAL DISORDERS … "   APPLI… " 4 (  8.5%… " 7 (  9.1%)"    " 5 (  6.6%)"                  1           2
 5 INFECTIONS AND INF… "INFECTIO… " 5 ( 10.6%… " 4 (  5.2%)"    " 3 (  3.9%)"                  1           3
 6 INFECTIONS AND INF… "   UPPER… " 4 (  8.5%… " 1 (  1.3%)"    " 1 (  1.3%)"                  1           3
 7 SKIN AND SUBCUTANE… "SKIN AND… " 7 ( 14.9%… "21 ( 27.3%)"    "26 ( 34.2%)"                  1           4
 8 SKIN AND SUBCUTANE… "   ERYTH… " 4 (  8.5%… " 3 (  3.9%)"    " 2 (  2.6%)"                  1           4
 9 SKIN AND SUBCUTANE… "   PRURI… " 3 (  6.4%… " 8 ( 10.4%)"    " 7 (  9.2%)"                  1           4
10 Total               "Total"    "47 (100.0%… "77 (100.0%)"    "76 (100.0%)"                  1           5
# … with 1 more variable: ord_layer_2 <dbl>

elimillera avatar Mar 29 '22 03:03 elimillera

@elimillera The functionality looks right now - but I have a suggestion for the API so I'm curious of your thoughts on that.

mstackhouse avatar Jun 06 '22 02:06 mstackhouse