在 kable() 的数字中的每一位添加逗号

Add commas every digits in numbers of kable()

我有下面的数据框,我用它创建了一个 kable。如何在每 3 位数字之间添加逗号?

Descs<-structure(list(Mean = c(NaN, 943330388, NaN, NaN, NaN, 543234645, 
45831420, NaN, 27301292, 160818771), Median = c(NaN, 943330388, 
NaN, NaN, NaN, 543234645, 45831420, NaN, 27301292, 160818771), 
    SD = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MAD = c(NA, 
    0, NA, NA, NA, 0, 0, NA, 0, 0), MIN = c(NA, 943330388, NA, 
    NA, NA, 543234645, 45831420, NA, 27301292, 160818771), MAX = c(NA, 
    943330388, NA, NA, NA, 543234645, 45831420, NA, 27301292, 
    160818771), VAR = c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
    ), RANGE = structure(c(NA, 943330388, NA, NA, NA, 543234645, 
    45831420, NA, 27301292, 160818771, NA, 943330388, NA, NA, 
    NA, 543234645, 45831420, NA, 27301292, 160818771), .Dim = c(10L, 
    2L)), QUANTILES = structure(c(NA, 943330388, NA, NA, NA, 
    543234645, 45831420, NA, 27301292, 160818771, NA, 943330388, 
    NA, NA, NA, 543234645, 45831420, NA, 27301292, 160818771), .Dim = c(10L, 
    2L), .Dimnames = list(NULL, c("25%", "75%")))), row.names = c("Comedy", 
"Education", "Entertainment", "Film & Animation", "Gaming", "Howto & Style", 
"Music", "People & Blogs", "Science & Technology", "Sports"), class = "data.frame")

library(kableExtra)

kable(Descs) %>%
    kable_styling(
      font_size = 15,
      bootstrap_options = c("striped", "hover", "condensed")
    ) 

您可以使用 kable 格式参数,这样可以避免在将数据放入 table 之前弄乱数据。

如果你想清除 NA 和 NaN,你可以在这行代码中添加:options(knitr.kable.NA = '')

library(kableExtra)

kable(Descs,
      format.args = list(big.mark = ",")) %>%
  kable_styling(
    font_size = 15,
    bootstrap_options = c("striped", "hover", "condensed")
  ) 

您可以使用此代码:

library(kableExtra)
library(dplyr)

Descs <- apply(Descs, 2, function(x) prettyNum(x, big.mark = ","))

kable(Descs) %>%
  kable_styling(
    font_size = 15,
    bootstrap_options = c("striped", "hover", "condensed")
  ) 

输出: