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

Converting etable to huxtable leaves duplicate cells empty #105

Open
louwerse opened this issue Feb 23, 2023 · 2 comments
Open

Converting etable to huxtable leaves duplicate cells empty #105

louwerse opened this issue Feb 23, 2023 · 2 comments

Comments

@louwerse
Copy link

When converting an etable to a huxtable duplicate cells are left empty. In the below example the same value appears in a cross table twice and the second cell is left empty after the conversion to a huxtable (in the below example this is Male, Past Smoker). Apparently, the attempts to remove duplicate labels are a bit too aggressive, deleting some table contents in specific situations like these?

library(tidyverse, warn.conflicts = FALSE)
library(expss, warn.conflicts = FALSE)
#> Loading required package: maditr
#> 
#> To aggregate several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)
#> 
#> Attaching package: 'maditr'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, coalesce, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
#> The following object is masked from 'package:readr':
#> 
#>     cols
#> 
#> Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
#>  To return to the console output, use 'expss_output_default()'.
library(huxtable, warn.conflicts = FALSE)

# Create some example data with equal observed frequencies in one column
data <- data.frame(
  "Male" = c(14, 14, 16),  
  "Female" = c(12, 14, 19), 
  row.names = c("Never smoked", "Past smoker", "Current smoker"),
  stringsAsFactors = FALSE)
# colnames(data) <- c("Male", "Female")
x <- c()
for (row in rownames(data)) {
  for (col in colnames(data)) {
    x <- rbind(x, matrix(rep(c(row, col), data[row, col]), ncol = 2, byrow = TRUE))
  }
}
df <- as.data.frame(x)
colnames(df) <- c("Smoking", "Gender") #row, column

# Create etable
table <- df |>
  tab_cells(`Smoking`= Smoking) |>
  tab_cols(`Gender`= Gender, total(Gender)) |>
  tab_total_label("Total") |>
  tab_stat_cpct() |>
  tab_pivot() |>
  drop_empty_rows() |>
  drop_empty_columns() 
table
 Gender     #Total 
 Female   Male   
 Smoking 
   Current smoker  42.2 36.4   39.3
   Never smoked  26.7 31.8   29.2
   Past smoker  31.1 31.8   31.5
   #Total  45 44   89
# Transform into huxtable
huxtable <- as_huxtable(table) 
print(huxtable)
#>                                          Gender          #Total  
#>                                          Female   Male           
#>                 Smoking   Current          42.2   36.4     39.3  
#>                           smoker                                 
#>                           Never smoked     26.7   31.8     29.2  
#>                           Past smoker      31.1            31.5  
#>                           #Total           45     44       89    
#> 
#> Column names: V1, V2, V3, V4, V5

Created on 2023-02-23 with reprex v2.0.2

@gdemin
Copy link
Owner

gdemin commented Feb 28, 2023

Thank you for detailed description. Yes, it's a bug. I will fix it with the next version

@louwerse
Copy link
Author

Thank you for your prompt reply!

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