0

I would like to generate a descriptive statistic by subgroup (gender of hh head) in R that is than printed into a word document.

I tried to use gtsummary but ended up not really understanding how it works. One problem that occured was that the statistics were not summarised above all surveyed households. The table rather showed each household individually.

structure(list(HHID = c("000a1b48-1223-4e6c-8d67-6a1c73f8b433", 
"00147ada-cbd4-4bc9-8b65-b07ec403cf7e", "00165d72-086b-4490-9d5d-82446d9a652f", 
"002552a4-59d7-4244-bc7f-f749e056d442", "00334d35-5375-4689-b39e-f000ec097d08", 
"005c8a23-00f7-4a13-a7fa-b991fd597b2e"), computer = c(1, 0, 0, 
0, 0, 0), bulb = c(1, 1, 1, 1, 1, 1), charger = c(1, 1, 1, 0, 
1, 1), radio = c(0, 0, 0, 0, 0, 0), tv = c(1, 1, 0, 0, 1, 1), 
    fan = c(1, 1, 1, 1, 1, 0), fridge = c(1L, 1L, 0L, 0L, 1L, 
    1L), therm_app = c(1L, 0L, 1L, 0L, 1L, 0L), mech_app = c(1L, 
    0L, 1L, 0L, 1L, 0L), high_edu_male = c(17, 15, 12, 14, 16, 
    16), numhh_mem_young = c(3L, 2L, 4L, 3L, 0L, 2L), hours_grid = c(NA, 
    3L, 12L, 15L, 15L, 4L), gen_head = c(0, 0, 0, 0, 0, 1), high_edu_female     = c(16, 9, 10, 12, 16, 12), dec_expensive = c(1, 1, 1, 1, 0, 1), 
    dec_clothes = c(1, 0, 1, 0, 0, 0), dec_market = c(1, 0, 1, 
    0, 0, 0), dec_dur = c(1, 0, 1, 0, 0, 0), dec_stove = c(1, 
    1, 1, 1, 0, 1), num_chairs = c(6L, 5L, 2L, 8L, 7L, 4L), num_bic = c(1L, 
    0L, 0L, 0L, 0L, 0L), num_motorbi = c(0L, 0L, 1L, 2L, 1L, 
    0L), num_car = c(2L, 0L, 0L, 0L, 1L, 0L), hh_expen = c(20000L, 
    5000L, 8000L, 15000L, 15000L, 15000L), own_home = c(0, 1, 
    0, 1, 0, 1), hhmem_room = c(1, 1.33333333333333, 1.16666666666667, 
    0.625, 1.5, 1)), row.names = c(NA, 6L), class = "data.frame")

This is the command I tried:

as_tibble(all_summary)
all_summary %>% tbl_summary(by = gen_head)
print(all_summary)
Ulli
  • 3
  • 4
  • 1
    Please read about [how to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and update your question accordingly. Include a sample of your data by pasting the output of `dput()` into your post or `dput(head())` if you have a large data frame. Also include code you have tried, any relevant errors, and expected output. If you cannot post your data, then post code for creating representative data. [Do not post images of code and/or data.](https://meta.stackoverflow.com/a/285557/6382434). – LMc Aug 29 '23 at 19:59
  • 1
    I think this question should be re-opened since you have made edits to improve the quality of your question. Is `all_summary` the `dput` output in your post? Or were there intermediate steps you took to try and create it? If the latter, please add those steps. – LMc Aug 29 '23 at 21:00
  • I agree that more information about the data and what you want your final table to look like would be helpful. – Jay Bee Aug 30 '23 at 05:51
  • You may like to try the `table1` package. For example something like `table1(~ fan + fridge + therm_app + num_chairs | gen_head, data = df)` could be along the lines of what you are after. – Jay Bee Aug 30 '23 at 05:52
  • Thank you! `all_summary` is the data frame I want to use to get the summary statistics. I would like to have summary statistics from all of the variables but by the gender of the household head `gen_head`. – Ulli Aug 30 '23 at 06:27

0 Answers0