1

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

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

我只是想把它变成一个表格,其中行名是玩家的,列名是“25th percentile, 50th percentile”等。

谢谢!

4

1 回答 1

2

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

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)

在此处输入图像描述

于 2021-03-01T03:54:14.810 回答