0

I am completely stuck with creating a table out of a following code:

table4a = NY %>% 
  group_by(loan_type) %>% 
  summarize(
    min_amt = min(loan_amount, na.rm=TRUE), 
    max_amt = max(loan_amount, na.rm=TRUE), 
    mean_amt = mean(loan_amount, na.rm = TRUE))
table4a <- as.data.table(table4a)

Basically, I need the min, max, and mean of amounts by type of loan in a table, but it's just not working; I know I can create separate mins and maxs and means by loan type and plug them into a matrix, but I feel like there has got to be a better way! Please help!

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Kay Sh
  • 1
  • 1
    What does "not working" mean exactly here? Are you getting an error of some kind? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 13 '20 at 23:46
  • I'm not getting a syntax error; rather, I am trying to get an output that looks like a formatted table that I could export and include in a paper (i.e., with proper headers, etc.). – Kay Sh Nov 13 '20 at 23:58
  • 2
    Well, since we can't run the code it's unclear what your current output is and what you need the output to look like. A [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would make it much easier to help you. What exactly is a "proper" header? – MrFlick Nov 13 '20 at 23:59

1 Answers1

0

It's hard to give advice without more details, but look into the gt package, e.g.

library(tidyverse)
#install.packages("palmerpenguins")
library(palmerpenguins)
#install.packages("gt")
library(gt)

penguins %>% 
  group_by(species) %>%
  na.omit() %>% 
  mutate(Minimum = as.numeric(min(flipper_length_mm)),
         Maximum = max(bill_length_mm),
         Mean = mean(bill_depth_mm)) %>% 
  select(species, Minimum, Maximum, Mean) %>% 
  unique() %>% 
  gt(groupname_col = TRUE)

example.png

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46