Data Science Desktop Survival Guide by Graham Williams Desktop Survival Project Home Preface Data Science Introducing R R Constructs R Tasks R Strings R Read, Write, and Create Data Template Data Exploration Data Wrangling Data Visualisation Statistics ML Template ML Scenarios ML Activities ML Applications ML Algorithms Cluster Analysis Decision Trees Computer Vision Graph Data Privacy Literate Data Science Coding with Style Resources Bibliography Index

## Formatting Tables Using XTable

Raw Whilst knitr::kable() provides basic functionality much more extensive control over the formatting of tables is provided by xtable. By default the table produced is called a floating table so that it floats within the document to an appropriate location.

As a floating table we will add a caption= and a table reference label= to the table so that they do not get lost. We can then refer to the table within the text and have the tables appear somewhere convenient automatically. The code block below, for example, produces Table 22.1 on page . The table and page numbers are automatically assigned to the table. Within LaTeX we can access the table number using \ref{egtbl} (22.1) and the page number using \pageref{egtbl} ().

# Load the package from the local library into the R session.

library(xtable)

# Generate a floating table with a caption.

ds %>% xtable(caption="Example xtable.", label="egtbl")

Table 22.1: Example xtable.
 Location MinTemp MaxTemp Rainfall Evaporation 1 Tuggeranong 10.00 25.10 0.20 2 Wollongong 20.80 30.70 1.00 3 Dartmoor 5.70 14.90 1.60 2.60 4 Sale 16.80 28.80 0.00 6.00 5 WaggaWagga 7.70 31.30 0.00 4.00

Also note that by default missing values (NA) are not printed nor are the extra lines that are printed by default when using knitr::kable().

There are very many formatting options available for fine tuning how the table is to be presented and we cover some of these in the following pages. We also note that some options are provisioned by xtable::xtable() whilst others are available through xtable::print.xtable(). An example option is include.rownames= which is an option available with xtable::print.xtable(). The result is seen in Table 22.2.

# Display a table without row names.

ds %>%
xtable(caption="Remove row numbers.", label="tblnonums") %>%
print(include.rownames=FALSE)

Table 22.2: Remove row numbers.
 Location MinTemp MaxTemp Rainfall Evaporation Tuggeranong 10.00 25.10 0.20 Wollongong 20.80 30.70 1.00 Dartmoor 5.70 14.90 1.60 2.60 Sale 16.80 28.80 0.00 6.00 WaggaWagga 7.70 31.30 0.00 4.00