Data Science Desktop Survival Guide
by Graham Williams
Formatting Numbers with XTable
Raw 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 22.3 we have removed all decimal points.
# Display a table removing digits from numbers.
, caption="Decimal points."
, label="tbldp0") %>%
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.
, caption="Large numbers."
, label="tbllrg") %>%
Consider the result in Table 22.4. It is difficult to distinguish between the thousands and millions. We often find ourselves having to carefully count the digits to check whether 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 22.5.
# Format large numbers using commas as appropriate.
, caption="Large numbers formatted."
, label="tbllrgf") %>%