I'm using the modelsummary package in R to generate a summary of my model, and I'd like to format the number of observations (nobs) with commas as thousand separators. I've tried several approaches, but none have worked so far. Here's what I've attempted:
format_nobs <- function(x) {
if (is.numeric(x)) {
return(format(x, big.mark = ",", scientific = FALSE))
}
return(x)
}
And in the gof_map:
gm <- tibble::tribble(
~raw, ~clean, ~fmt,
"nobs", "N", format_nobs,
"adj.r.squared", "Adj. R²", 2
)
This resulted in an error: Error in x$raw : $ operator is invalid for atomic vectors.
Using a sprintf format string:
gm <- tibble::tribble(
~raw, ~clean, ~fmt,
"nobs", "N", "%'.0f",
"adj.r.squared", "Adj. R²", "%.2f"
)
This resulted in the error: Error in sprintf(fmt, k) : invalid format '%'.0f'; use format %d, %i, %o, %x oder %X for integer objects.
I would like to know how to properly format the number of observations in the modelsummary output to include commas as thousand separators. Any suggestions or alternative methods to achieve this would be greatly appreciated.
You can use the list syntax illustrated on the
modelsummarywebsite: https://modelsummary.com/vignettes/modelsummary.html#gof_map