Git Product home page Git Product logo

Comments (4)

ewenharrison avatar ewenharrison commented on July 29, 2024

Thanks for this.
This is a pivoted stratified table. So stratify in the normal manner and then pivot.
You will need library(gt) or similar to sort your final formatting, but finalfit will give you all the numbers you need.
Something like this.
If this was useful to people it could be wrapped into a neat function.
All you should need to do is change the explanatory, dependent and split variable names.
Let me know if you have issues.

library(finalfit)
library(tidyverse)

# Table rows
explanatory = c("obstruct.factor", "perfor.factor")

# Table columns
dependent = "rx.factor"
split = c("sex.factor", "age.factor")

# Piped function to generate stratified crosstabs table
colon_s %>%
  group_by(!!! syms(split)) %>% 
  group_modify(~ summary_factorlist(.x, dependent, explanatory)) %>%
  mutate(label = na_if(label, "")) %>% 
  fill(label) %>% 
  pivot_wider(names_from =  split, 
              values_from = colon_s %>% pull(dependent) %>% levels()) %>% 
  mutate(label = rm_duplicates(label)) %>% 
  as.data.frame()

        label levels Obs_Female_<40 years Obs_Female_40-59 years Obs_Female_60+ years Obs_Male_<40 years Obs_Male_40-59 years
1 Obstruction     No             8 (66.7)              50 (80.6)            57 (79.2)           7 (58.3)            51 (83.6)
2                Yes             4 (33.3)              12 (19.4)            15 (20.8)           5 (41.7)            10 (16.4)
3 Perforation     No           12 (100.0)              60 (95.2)            73 (98.6)          12 (92.3)            60 (98.4)
4                Yes              0 (0.0)                3 (4.8)              1 (1.4)            1 (7.7)              1 (1.6)
  Obs_Male_60+ years Lev_Female_<40 years Lev_Female_40-59 years Lev_Female_60+ years Lev_Male_<40 years Lev_Male_40-59 years
1          75 (83.3)             9 (90.0)              35 (71.4)            59 (81.9)           7 (77.8)            45 (70.3)
2          15 (16.7)             1 (10.0)              14 (28.6)            13 (18.1)           2 (22.2)            19 (29.7)
3          89 (96.7)           10 (100.0)              48 (96.0)            70 (95.9)           8 (88.9)            63 (96.9)
4            3 (3.3)              0 (0.0)                2 (4.0)              3 (4.1)           1 (11.1)              2 (3.1)
  Lev_Male_60+ years Lev+5FU_Female_<40 years Lev+5FU_Female_40-59 years Lev+5FU_Female_60+ years Lev+5FU_Male_<40 years
1          88 (87.1)                 9 (56.2)                  43 (81.1)                76 (83.5)               9 (90.0)
2          13 (12.9)                 7 (43.8)                  10 (18.9)                15 (16.5)               1 (10.0)
3         101 (98.1)               16 (100.0)                  52 (98.1)                91 (96.8)             10 (100.0)
4            2 (1.9)                  0 (0.0)                    1 (1.9)                  3 (3.2)                0 (0.0)
  Lev+5FU_Male_40-59 years Lev+5FU_Male_60+ years
1                41 (85.4)              63 (82.9)
2                 7 (14.6)              13 (17.1)
3                51 (98.1)              76 (96.2)
4                  1 (1.9)                3 (3.8)

from finalfit.

andresimi avatar andresimi commented on July 29, 2024

thanx! that solved the problem

from finalfit.

andresimi avatar andresimi commented on July 29, 2024

Actually, I have another question: can I get weighted frequencies for this table?

from finalfit.

ewenharrison avatar ewenharrison commented on July 29, 2024

See this discussion
#13

from finalfit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.