Aligning Columns with knitr kable function

53.4k views Asked by At

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?

4

There are 4 answers

3
lmo On

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.
0
Alexandre Gareau On

While looking for an answer for aligning specific column in kabble, I realized that now in kable 1.3.4 (I don't know if it was there before), you can just put "l" "c" "r" for the alignment following this RAW format.

In this example, the first 4 columns are left-aligned, and the 4 subsequent columns are center-align.

kable(align = "llllcccc")

This if the documentation that support this code.

align -> Column alignment: a character vector consisting of 'l' (left), 'c' (center) and/or 'r' (right). By default or if align = NULL, numeric columns are right-aligned, and other columns are left-aligned. If length(align) == 1L, the string will be expanded to a vector of individual letters, e.g. 'clc' becomes c('c', 'l', 'c'), unless the output format is LaTeX.

1
tchevrier On

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

0
Ryan Bradley On

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])))