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

Table with more than 2 dependent variables in finalfit #54

Open
andresimi opened this issue Sep 2, 2020 · 4 comments
Open

Table with more than 2 dependent variables in finalfit #54

andresimi opened this issue Sep 2, 2020 · 4 comments

Comments

@andresimi
Copy link

Is it possible to create a table with more than 2 dependent variables in finalfit?

I as looking for something like this:

image

@ewenharrison
Copy link
Owner

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)

@andresimi
Copy link
Author

thanx! that solved the problem

@andresimi
Copy link
Author

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

@ewenharrison
Copy link
Owner

See this discussion
#13

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