如何在 R 中使用 gt() 制作 table?

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

所以,我有这些数据,我正在尝试将其格式化为一个漂亮的 table。目前,我一直在使用 knitr pacakge 中的 kable() 命令,但我正在尝试学习如何制作漂亮漂亮的 table 看起来更专业。我有以下代码:

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

我只是想把它变成 table,其中行名称是玩家的名称,列名称是“第 25 个百分位数、第 50 个百分位数”等

谢谢!

gt 需要一个 data.frame 或 tibble 对象。 Player_distribution_table 是一个矩阵(因为你使用了 cbind)。您可以使用 rownames_to_stub = TRUE 将数据帧传递给 gt 函数以获取玩家名称。

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)