atorus-research / Tplyr

Home Page:https://atorus-research.github.io/Tplyr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Treatment variable factor levels not respected when using `add_total_group()`/`add_treat_grps()`

mlindner00120 opened this issue · comments

Description

If a study has serious adverse events only in one treatment group (e.g. Placebo), there are problems to display "Verum", "Placebo" and Total at the same time.

  • If add_total_group is left out, Placebo and Verum are displayed as defined in the factors,
  • If add_total_group is included, the Verum column disappears.

Steps to Reproduce (Bug Report Only)

library(tidyverse)
library(Tplyr)

test <- tibble(treatment = "Placebo",
relationship="Not related") %>%
mutate(treatment=factor(
treatment, levels=c("Verum", "Placebo")),
relationship=factor(
relationship, levels=c("Not related","Possible","Unlikely","Any")
)
)

tab <- tplyr_table(test, treatment) %>%
add_layer(group_count(relationship)) %>%
add_total_group %>%
build()

Expected behavior: [What you expected to happen]
I would expect three columns:
var1_Verum
var1_Placebo
var1_Total

Actual behavior: [What actually happened]
if add_total_group is left out I get the expected behaviour:
var1_Placebo
var1_Verum

if add_total_group is included I get only:
var1_Placebo
var1_Total

Example of Table (Feature Request Only)

Versions

You can get this information from executing sessionInfo().
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=English_Switzerland.1252 LC_CTYPE=English_Switzerland.1252
[3] LC_MONETARY=English_Switzerland.1252 LC_NUMERIC=C
[5] LC_TIME=English_Switzerland.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] forcats_0.5.2 stringr_1.4.1 dplyr_1.0.10 purrr_0.3.5 readr_2.1.3
[6] tidyr_1.2.1 tibble_3.1.8 ggplot2_3.3.6 tidyverse_1.3.2 Tplyr_0.4.4
[11] magrittr_2.0.3

loaded via a namespace (and not attached):
[1] pillar_1.8.1 compiler_4.1.1 cellranger_1.1.0 dbplyr_2.2.1
[5] tools_4.1.1 lubridate_1.8.0 jsonlite_1.8.2 googledrive_2.0.0
[9] lifecycle_1.0.3 gargle_1.2.1 gtable_0.3.1 pkgconfig_2.0.3
[13] rlang_1.0.6 reprex_2.0.2 DBI_1.1.3 cli_3.4.1
[17] rstudioapi_0.14 haven_2.5.1 xml2_1.3.3 withr_2.5.0
[21] httr_1.4.4 hms_1.1.2 generics_0.1.3 vctrs_0.4.2
[25] fs_1.5.2 googlesheets4_1.0.1 grid_4.1.1 tidyselect_1.2.0
[29] glue_1.6.2 R6_2.5.1 fansi_1.0.3 readxl_1.4.1
[33] tzdb_0.3.0 modelr_0.1.9 ellipsis_0.3.2 backports_1.4.1
[37] scales_1.2.1 rvest_1.0.3 assertthat_0.2.1 colorspace_2.0-3
[41] utf8_1.2.2 stringi_1.7.6 munsell_0.5.0 broom_1.0.1
[45] crayon_1.5.2

Thanks for the report @mlindner00120. I can confirm that this is in fact a bug, so we'll pursue a fix.

For this specific table type, if you use set_pop_data() and related functions (assuming the population data has all the treatment groups), you'll get the proper columns in your result.

Hi! Thanks for the reply and the fix as well as the workaround. I am looking forward to it. Best Greetings from Switzerland.

@mlindner00120 this issue has officially been fixed on CRAN.

Very good :-)