Aligning Columns with knitr kable function

36,118

Solution 1

You want to feed kable a vector of alignment strings equal to the number of columns. As mentioned in the help file,

the alignment of columns: a character vector consisting of 'l' (left), 'c' (center) and/or 'r' (right); by default, numeric columns are right-aligned, and other columns are left-aligned; if align = NULL, the default alignment is used.

Here is a reproducible example.

Without any alignment values, character columns are left-aligned and numeric columns are right-aligned as you can see below.

library(knitr)

kable(head(mtcars[1:5]))

which returns

|                  |  mpg| cyl| disp|  hp| drat|
|:-----------------|----:|---:|----:|---:|----:|
|Mazda RX4         | 21.0|   6|  160| 110| 3.90|
|Mazda RX4 Wag     | 21.0|   6|  160| 110| 3.90|
|Datsun 710        | 22.8|   4|  108|  93| 3.85|
|Hornet 4 Drive    | 21.4|   6|  258| 110| 3.08|
|Hornet Sportabout | 18.7|   8|  360| 175| 3.15|
|Valiant           | 18.1|   6|  225| 105| 2.76|

To get the numeric columns center-aligned, while keeping the character column right aligned, I used the following.

kable(head(mtcars[1:5]), align=rep('c', 5))


|                  | mpg  | cyl | disp | hp  | drat |
|:-----------------|:----:|:---:|:----:|:---:|:----:|
|Mazda RX4         | 21.0 |  6  | 160  | 110 | 3.90 |
|Mazda RX4 Wag     | 21.0 |  6  | 160  | 110 | 3.90 |
|Datsun 710        | 22.8 |  4  | 108  | 93  | 3.85 |
|Hornet 4 Drive    | 21.4 |  6  | 258  | 110 | 3.08 |
|Hornet Sportabout | 18.7 |  8  | 360  | 175 | 3.15 |
|Valiant           | 18.1 |  6  | 225  | 105 | 2.76 |

The following text, if copied into an .Rmd file, will return the table, formatted as desired as a pdf file.

---
title: "Untitled"
output: pdf_document
---

this thing

```{r table1, as.is=TRUE}
library(knitr)

kable(head(mtcars[1:5]))

```


is not a centered table, while this thing

```{r table2, as.is=TRUE}

kable(head(mtcars[1:5]), align=rep('c', 5))

```

is a centered table.

Solution 2

Even easier: kable(mtcars, align = "l") works just fine!

Solution 3

You can also use the length function to create a vector for all rows in a vector/dataframe without having to hard code a length. The example below left-aligns all the columns in the mtcars data frame no matter the length.

 kable(mtcars, align=rep('l', length(mtcars[,1])))
Share:
36,118
Kivis
Author by

Kivis

Updated on December 05, 2020

Comments

  • Kivis
    Kivis over 3 years

    I am performing a simple task: creating a table and outputting it using R Markdown to pdf as follows:

    library(knitr)
    kable(datatable,align='ccccccc',
    col.names = c("Copy","Sigma Est","Sigma Lower","Sigma Upper", 
    "Lambda Est","Lambda Lower","Lambda Upper"),digits=3)
    

    Problem

    when I output the table, columns are not centered. Actually, for some tables they are right aligned for others - left aligned, which seems rather random to me.

    Question

    How can I control the alignment of columns with R function kable from package knitr, i.e., what am I doing wrong?

    EDIT

    Running:

    library(knitr)
    kable(datatable,align=c(rep('c',times=7)),
    col.names = c("Copy","Sigma Est","Sigma Lower","Sigma Upper", 
    "Lambda Est","Lambda Lower","Lambda Upper"),digits=3)
    

    Yields:

    | Copy | Sigma Est | Sigma Lower | Sigma Upper | Lambda Est | Lambda Lower | Lambda Upper |
    |:----:|:---------:|:-----------:|:-----------:|:----------:|:------------:|:------------:|
    |  0   |  14.631   |   12.275    |   16.987    |   0.129    |    8.778     |    9.296     |
    |  1   |  16.988   |   14.275    |   19.700    |   0.136    |    8.190     |    8.736     |
    |  2   |  20.850   |   17.517    |   24.183    |   0.129    |    8.595     |    9.113     |
    |  3   |  20.551   |   17.229    |   23.874    |   0.127    |    9.015     |    9.523     |
    |  4   |  22.651   |   18.993    |   26.310    |   0.127    |    8.969     |    9.478     |
    |  5   |  23.369   |   19.652    |   27.086    |   0.127    |    8.599     |    9.108     |
    

    This is exactly what I want as :---: denotes centering of columns, however, when I press Knit PDF and a pdf document is produced, all columns are still left-aligned. How do I go around that?