Go to TogaWare.com Home Page. Data Science Desktop Survival Guide
by Graham Williams
Duck Duck Go

Formatting Tables Using Kable

Raw Including a typeset table based on a dataset can be accomplished using knitr::kable(). Here we will use the larger weatherAUS dataset from rattle setting it up as a dplyr::tibble() courtesy of dplyr. We will then choose specific columns and a random selection of rows to include in the table. The source text we include in our .Rnw file is listed in the following code block.

<<example_kable, echo=TRUE, results="asis">>=
set.seed(42)
library(rattle)
nobs <- nrow(weatherAUS)
obs  <- sample(nobs, 5)
vars <- 2:6
ds   <- weatherAUS[obs, vars]
kable(ds)
@

The result (also showing the R code since we specified echo=TRUE) is then:

# Set the seed so that results are repeatable.

set.seed(42)

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

library(rattle)

# Record metadata for a sample of the dataset.

nobs <- nrow(weatherAUS)
obs  <- sample(nobs, 5)
vars <- 2:6
ds   <- weatherAUS[obs, vars]

# Generate the appropriate LaTeX code to display the data.

kable(ds)

Location MinTemp MaxTemp Rainfall Evaporation
Tuggeranong 10.0 25.1 0.2 NA
Wollongong 20.8 30.7 1.0 NA
Dartmoor 5.7 14.9 1.6 2.6
Sale 16.8 28.8 0.0 6.0
WaggaWagga 7.7 31.3 0.0 4.0

Since we are working with a random sample and we would like the sampling to be repeatable we have used base::set.seed() to initialise the random number generator to a fixed value.


Support further development by purchasing the PDF version of the book.
Other online resources include the GNU/Linux Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 2000-2020 Togaware Pty Ltd. . Creative Commons ShareAlike V4.