26.15 Formatting Numbers with XTable

REVIEW As with knitr::kable() we can limit the number of digits displayed to avoid giving an impression of a high level of accuracy or to simplify the presentation. In Table ?? we have removed all decimal points.

# Display a table removing digits from numbers.

ds %>%
xtable(digits=0
, caption="Decimal points."
, label="tbldp0") %>%
print(include.rownames=FALSE)
% latex table generated in R 4.1.0 by xtable 1.8-4 package % Sat Jul 31 17:45:21 2021

When we have large numbers being displayed it is imperative that we include commas to separate the thousands. Very many mistakes are made misreading numbers that include many digits when commas are not included.

# Take a copy of the dataset so as to change the data.

dst     <- ds %>% sample_frac(0.01)

# Randomly create very large numbers across all but the first variable.

dst[-1] <- sample(10000:99999, nrow(dst)) * dst[-1]

# Illustrate the default table display of large numbers.

dst %>%
xtable(digits=0
, caption="Large numbers."
, label="tbllrg") %>%
print(include.rownames=FALSE)
% latex table generated in R 4.1.0 by xtable 1.8-4 package % Sat Jul 31 17:45:21 2021

Consider the result in Table ??. It is difficult to distinguish between the thousands and millions. We often find ourselves having to carefully count the digits to check whether # A tibble: 1 x 1, MaxTemp, , 1 NA really is # A tibble: 1 x 1, MaxTemp, , 1 NA. To avoid this cognitive load on the reader we should always use a comma to separate the thousands and millions. This simple principle makes it much easier for the reader to appreciate the scale and to avoid misreading data, yet it is so often overlooked. We can see the result in Table ??.

# Format large numbers using commas as appropriate.

dst %>%
xtable(digits=0
, caption="Large numbers formatted."
, label="tbllrgf") %>%
print(include.rownames=FALSE,
format.args=list(big.mark=","))
% latex table generated in R 4.1.0 by xtable 1.8-4 package % Sat Jul 31 17:45:21 2021

Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984.