1

I´m trying to create a table with mean+SD and median+IQR value printed under the mean, but I'm struggling to break a line in one cell.

Table should look somewhat like this:

#gene     1               2
#value1   mean(SD)        mean(SD)
#         median(IQR)     median(IQR)
#value2   mean(SD)        mean(SD)
#         median(IQR)     median(IQR)

This is the code line I've so far produced, I've tried to use multiple expressions for line break (" ", "\n", or " ", "\newline", etc. but nothing worked) :


analysis <- df%>%group_by(gene)%>%
  summarise_all(list(~str_c(round(mean(., na.rm=TRUE),2),"(",round(sd(.,na.rm=TRUE),2),")", " ", "\\n", 
                            round(median(., na.rm = TRUE), 2), "(" ,round(IQR(.,na.rm = TRUE),2),")")))

The code above prints this:

#gene     1                            2
#value1   mean(SD) \nmedian(IQR)        mean(SD) \nmedian(IQR) 
#value2   mean(SD) \nmedian(IQR)        mean(SD) \nmedian(IQR) 
  

Does anybody knows how to fix this?

371.
  • 23
  • 5
  • What is the output medium? How is the table supposed to be printed? e.g. PDF or HTML via RMarkdown? Or really just printing in the R console? If it's the former look for a table formatting package that suits your constraints: kable, kableExtra, flextable, gt, ... If it's the latter, I'd hack around producing two rows per group in the summary – Aurèle Oct 27 '22 at 13:27
  • I'm using gt package. – 371. Oct 28 '22 at 07:53

1 Answers1

0

Using gt (per OP's comment):

analysis %>% 
  mutate(across(everything(), str_replace_all, fixed("\\n"), "<br>")) %>% 
  gt() %>% 
  fmt_markdown(columns = everything())

enter image description here


Credit to https://stackoverflow.com/a/63355285/6197649

Aurèle
  • 12,545
  • 1
  • 31
  • 49