chi.square and table errors when using tidyverse

I am running into issues with X-squared

sessionInfo()
R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

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

other attached packages:
 [1] lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1  
 [4] dplyr_1.1.4     purrr_1.0.2     readr_2.1.5    
 [7] tidyr_1.3.1     tibble_3.2.1    ggplot2_3.5.1  
[10] tidyverse_2.0.0

loaded via a namespace (and not attached):
 [1] gtable_0.3.5      xfun_0.43         lattice_0.22-6   
 [4] tzdb_0.4.0        vctrs_0.6.5       tools_4.4.0      
 [7] generics_0.1.3    parallel_4.4.0    fansi_1.0.6      
[10] RSQLite_2.3.6     blob_1.2.4        pkgconfig_2.0.3  
[13] dbplyr_2.5.0      lifecycle_1.0.4   compiler_4.4.0   
[16] farver_2.1.2      munsell_0.5.1     codetools_0.2-20 
[19] yaml_2.3.8        pillar_1.9.0      MASS_7.3-60.2    
[22] cachem_1.1.0      parallelly_1.37.1 tidyselect_1.2.1 
[25] digest_0.6.35     stringi_1.8.3     future_1.33.2    
[28] listenv_0.9.1     labeling_0.4.3    rprojroot_2.0.4  
[31] fastmap_1.2.0     grid_4.4.0        here_1.0.1       
[34] colorspace_2.1-0  cli_3.6.2         magrittr_2.0.3   
[37] arsenal_3.6.3     vcd_1.4-12        utf8_1.2.4       
[40] broom_1.0.5       withr_3.0.0       scales_1.3.0     
[43] backports_1.4.1   bit64_4.0.5       timechange_0.3.0 
[46] globals_0.16.3    bit_4.0.5         zoo_1.8-12       
[49] hms_1.1.3         memoise_2.0.1     knitr_1.46       
[52] lmtest_0.9-40     rlang_1.1.3       glue_1.7.0       
[55] DBI_1.2.2         renv_1.0.7        pkgload_1.3.4    
[58] rstudioapi_0.16.0 R6_2.5.1  
X <- structure(list(statistic = c(`X-squared` = 7.39823375500443), 
    parameter = c(df = 1L), p.value = 0.00652879498007057, method = "Pearson's Chi-squared test with Yates' continuity correction", 
    data.name = "T1", observed = structure(c(0L, 13L, 38L, 49L
    ), dim = c(2L, 2L), dimnames = structure(list(c("no", "yes"
    ), c("FALSE", "TRUE")), names = c("", "")), class = "table"), 
    expected = structure(c(4.94, 8.06, 33.06, 53.94), dim = c(2L, 
    2L), dimnames = structure(list(c("no", "yes"), c("FALSE", 
    "TRUE")), names = c("", ""))), residuals = structure(c(-2.22261107708929, 
    1.74004078558058, 0.859162917926646, -0.672622634729506), dim = c(2L, 
    2L), class = "table", dimnames = structure(list(c("no", "yes"
    ), c("FALSE", "TRUE")), names = c("", ""))), stdres = structure(c(-3.0262723049611, 
    3.0262723049611, 3.0262723049611, -3.0262723049611), dim = c(2L, 
    2L), class = "table", dimnames = structure(list(c("no", "yes"
    ), c("FALSE", "TRUE")), names = c("", "")))), class = "htest")
> X$observed
Error in UseMethod("tally") : 
  no applicable method for 'tally' applied to an object of class "table"

I have no idea why tally would be called if X is already the results of a chi.square that I have brought in with an assignment from a dput of another session, my only guess is that one of the packages I have loaded is doing something weird

we can see that observed = structure(c(0L, 13L, 38L, 49L) so why is tally being called when I run

X$observed?

If I clear and restart my environment then X$observed returns as expected.

Can you recreate the error?

I am trying to come up with a reprex, I believe has something to do with dbplyr and the table function.

I have several dbplyr queries in a function calls to a database.

I am pulling samples of the data into the environment, then I am attempting to run table(dataframe$x, dataframe$y) and the error occurs.

I have tried base::table(dataframe$x, dataframe$y) but the error still persists.

I believe it has something to do with the dbplyr connection and the table function as when I have changed from tally to summarise in the functional query to the database the error changes from

  • Error in UseMethod("tally") : no applicable method for 'tally' applied to an object of class "table"

to

  • Error in UseMethod("summarise") : no applicable method for 'tally' applied to an object of class "table"

My current thought is to try to recreate the error by creating a small sqlite database for the reprex include my functions and see if I can get the error in reprex form.

Yes, that might be helpful. I'm not sure you'll be able to use the reprex addin since this involve creating files, but if you can least come up with a recipe (sequence of steps) for recreating the behavior, that would be good.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.