Skip to content

[Bug]: s_summary.factor when only missing values/level is present in data and option na_rm = TRUE #1431

@iaugusty

Description

@iaugusty

What happened?

A bug happened!

When all values of factor variable are missing level or NA, call to analyze_vars result in an error :

Error: Error applying analysis function (var - hp2): 'names' attribute [2] must be the same length as the vector [1]
occured at (row) path: root

Here is a reprex of the problem:

library(tern)
#> Loading required package: rtables
#> Loading required package: formatters
#> 
#> Attaching package: 'formatters'
#> The following object is masked from 'package:base':
#> 
#>     %||%
#> Loading required package: magrittr
#> 
#> Attaching package: 'rtables'
#> The following object is masked from 'package:utils':
#> 
#>     str
#> Registered S3 method overwritten by 'tern':
#>   method   from 
#>   tidy.glm broom

data <- mtcars
data$hp2 <- as.factor(NA)
data <- df_explicit_na(data, na_level = "<Missing>")

avars <- c("hp", "hp2")
lyt1 <- rtables::basic_table(show_colcounts = TRUE) %>% 
  tern::analyze_vars(vars = avars, 
                     show_labels = "visible", 
                     na_rm = FALSE, 
                     denom = "N_col", 
                     .stats = c("n", "mean_sd", 
                                "median", "count_fraction")
  )
table1 <- rtables::build_table(lyt = lyt1, df = data)
table1
#>                 all obs   
#>                  (N=32)   
#> ——————————————————————————
#> hp                        
#>   n                32     
#>   Mean (SD)   146.7 (68.6)
#>   Median         123.0    
#> hp2                       
#>   n                32     
#>   <Missing>    32 (100%)


lyt2 <- rtables::basic_table(show_colcounts = TRUE) %>% 
  tern::analyze_vars(vars = avars, 
                     show_labels = "visible", 
                     na_rm = TRUE, 
                     denom = "N_col", 
                     .stats = c("n", "mean_sd", 
                               "median", "count_fraction")
  )
table2 <- rtables::build_table(lyt = lyt2, df = data)
#> Error: Error applying analysis function (var - hp2): 'names' attribute [2] must be the same length as the vector [1]
#>  occured at (row) path: root
table2
#> Error: object 'table2' not found

x <- data$hp2
stats <- s_summary(x, na_rm = TRUE)
stats2 <- s_summary(x, na_rm = FALSE)
stats["count"]
#> $count
#> list()
stats2["count"]
#> $count
#> $count$`<Missing>`
#> count 
#>    32

The root cause is within s_summary.factor, stats["count"] should be list(NULL) and not just list().

sessionInfo()

R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Rocky Linux 8.10 (Green Obsidian)

Matrix products: default
BLAS/LAPACK: /usr/lib64/libopenblasp-r0.3.15.so;  LAPACK version 3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

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

other attached packages:
[1] reprex_2.1.1      tern_0.9.9        rtables_0.6.13    magrittr_2.0.3    formatters_0.5.11

loaded via a namespace (and not attached):
 [1] generics_0.1.4     tidyr_1.3.1        stringi_1.8.7      lattice_0.22-6     digest_0.6.37      evaluate_1.0.4     grid_4.4.1         RColorBrewer_1.1-3
 [9] pkgload_1.4.0      fastmap_1.2.0      Matrix_1.7-0       processx_3.8.6     backports_1.5.0    survival_3.8-3     ps_1.9.1           purrr_1.0.4       
[17] scales_1.4.0       Rdpack_2.6.4       cli_3.6.5          nestcolor_0.1.3    rlang_1.1.6        rbibutils_2.3      splines_4.4.1      withr_3.0.2       
[25] yaml_2.3.8         tools_4.4.1        checkmate_2.3.2    dplyr_1.1.4        ggplot2_3.5.2      forcats_1.0.0      broom_1.0.8        vctrs_0.6.5       
[33] R6_2.6.1           lifecycle_1.0.4    fs_1.6.6           pkgconfig_2.0.3    callr_3.7.6        pillar_1.11.0      gtable_0.3.6       rsconnect_1.3.4   
[41] glue_1.8.0         xfun_0.51          tibble_3.3.0       tidyselect_1.2.1   rstudioapi_0.17.1  knitr_1.49         farver_2.1.2       htmltools_0.5.8.1 
[49] rmarkdown_2.29     compiler_4.4.1

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingsme

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions