BonnieKlein
BonnieKlein

Reputation: 165

How to make a table with gt() in R?

So, I have this data I'm trying to format into a nice table. Currently, I've just been using the kable() command from the knitr pacakge, but I am trying to learn how to make nice and pretty tables that look more professional. I have the following code:

library(gt)
library(tidyverse)
library(glue)

Player <- c("Russel Westbrook", "James Harden", "Kawhi Leonard", "Lebron James",
            "Isaiah Thomas", "Stephen Curry", "Giannis Antetokounmpo", "John Wall",
            "Anthony Davis", "Kevin Durant")

Overall_proportion <- c(0.845, 0.847, 0.880, 0.674, 0.909, # q-the ratio of clutch makes 
                        0.898, 0.770, 0.801, 0.802, 0.875) # by clutch attempts

Clutch_makes <- c(64, 72, 55, 27, 75, # Y-values
                  24, 28, 66, 40, 13)

Clutch_attempts <- c(75, 95, 63, 39, 83, # Clutch_attempts -values
                     26, 41, 82, 54, 16)

NBA_stats <- as.data.frame(cbind(Player, Overall_proportion, Clutch_makes, Clutch_attempts))


# creating the various quartiles for the posterior distributions
q25    <- qbeta(0.250, Clutch_makes + 1, Clutch_attempts - Clutch_makes + 1)
q50    <- qbeta(0.500, Clutch_makes + 1, Clutch_attempts - Clutch_makes + 1)
q75    <- qbeta(0.750, Clutch_makes + 1, Clutch_attempts - Clutch_makes + 1)
q90    <- qbeta(0.900, Clutch_makes + 1, Clutch_attempts - Clutch_makes + 1)
q_low  <- qbeta(0.025, Clutch_makes + 1, Clutch_attempts - Clutch_makes + 1)
q_high <- qbeta(0.975, Clutch_makes + 1, Clutch_attempts - Clutch_makes + 1)

Player_distribution_table <- cbind(q25, q50, q75, q90, q_low, q_high)
rownames(Player_distribution_table) <- Player

I'm just trying to turn this into a table where the row names are those of the players, and the column names are "25th percentile, 50th percentile" etc.

Thank you!

Upvotes: 2

Views: 2558

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388992

gt needs a data.frame or tibble object. Player_distribution_table is a matrix (because you used cbind). You can pass dataframe to gt function with rownames_to_stub = TRUE to get player names.

Player_distribution_table <- data.frame(q25, q50, q75, q90, q_low, q_high)
rownames(Player_distribution_table) <- Player

gt::gt(Player_distribution_table, rownames_to_stub = TRUE)

enter image description here

Upvotes: 4

Related Questions