Table Show
Package “ztable” make everything possible about table. Basically, An object of “ztable” made from a data.frame. The default output format of ztable is RStudio::viewer or web-browser format(type=“viewer”). So if you want to use ztable in a “html” format, you should change the parameter ztable.type to “html”. If you want to use ztable in latex format, you should change the parameter ztable.type to “latex”.
library(ztable)
library(magrittr)
options(ztable.type="html")
z=ztable(head(iris))
z
|
Sepal.Length |
Sepal.Width |
Petal.Length |
Petal.Width |
Species |
1
|
5.10
|
3.50
|
1.40
|
0.20
|
setosa
|
2
|
4.90
|
3.00
|
1.40
|
0.20
|
setosa
|
3
|
4.70
|
3.20
|
1.30
|
0.20
|
setosa
|
4
|
4.60
|
3.10
|
1.50
|
0.20
|
setosa
|
5
|
5.00
|
3.60
|
1.40
|
0.20
|
setosa
|
6
|
5.40
|
3.90
|
1.70
|
0.40
|
setosa
|
|
You can change the position of data in each cell by adjusting the parameter “align”.
z=ztable(head(iris),align="cccccc")
z
|
Sepal.Length |
Sepal.Width |
Petal.Length |
Petal.Width |
Species |
1
|
5.10
|
3.50
|
1.40
|
0.20
|
setosa
|
2
|
4.90
|
3.00
|
1.40
|
0.20
|
setosa
|
3
|
4.70
|
3.20
|
1.30
|
0.20
|
setosa
|
4
|
4.60
|
3.10
|
1.50
|
0.20
|
setosa
|
5
|
5.00
|
3.60
|
1.40
|
0.20
|
setosa
|
6
|
5.40
|
3.90
|
1.70
|
0.40
|
setosa
|
|
You can change background color and font color with addRowColor() function.
z <- ztable(head(iris))
z <- addRowColor(z, rows=1,bg="#C90000",color="white")
print(z)
|
Sepal.Length |
Sepal.Width |
Petal.Length |
Petal.Width |
Species |
1
|
5.10
|
3.50
|
1.40
|
0.20
|
setosa
|
2
|
4.90
|
3.00
|
1.40
|
0.20
|
setosa
|
3
|
4.70
|
3.20
|
1.30
|
0.20
|
setosa
|
4
|
4.60
|
3.10
|
1.50
|
0.20
|
setosa
|
5
|
5.00
|
3.60
|
1.40
|
0.20
|
setosa
|
6
|
5.40
|
3.90
|
1.70
|
0.40
|
setosa
|
|
The pipe operator(“%>%”) from magrittr package can simplify your R code.
ztable(head(iris)) %>%
addRowColor(rows=1,bg="#C90000",color="white") %>%
print
|
Sepal.Length |
Sepal.Width |
Petal.Length |
Petal.Width |
Species |
1
|
5.10
|
3.50
|
1.40
|
0.20
|
setosa
|
2
|
4.90
|
3.00
|
1.40
|
0.20
|
setosa
|
3
|
4.70
|
3.20
|
1.30
|
0.20
|
setosa
|
4
|
4.60
|
3.10
|
1.50
|
0.20
|
setosa
|
5
|
5.00
|
3.60
|
1.40
|
0.20
|
setosa
|
6
|
5.40
|
3.90
|
1.70
|
0.40
|
setosa
|
|
You can add column groups to ztable using addcgroup function. The n.cgroup means how much columns included in each row group.
cgroup=c("Sepal","Petal","Species")
n.cgroup=c(2,2,1)
z <- ztable(head(iris)) %>%
addcgroup(cgroup=cgroup,n.cgroup=n.cgroup)
z
|
Sepal
|
|
Petal
|
|
Species
|
|
Sepal.Length |
Sepal.Width |
|
Petal.Length |
Petal.Width |
|
Species |
1
|
5.10
|
3.50
|
|
1.40
|
0.20
|
|
setosa
|
2
|
4.90
|
3.00
|
|
1.40
|
0.20
|
|
setosa
|
3
|
4.70
|
3.20
|
|
1.30
|
0.20
|
|
setosa
|
4
|
4.60
|
3.10
|
|
1.50
|
0.20
|
|
setosa
|
5
|
5.00
|
3.60
|
|
1.40
|
0.20
|
|
setosa
|
6
|
5.40
|
3.90
|
|
1.70
|
0.40
|
|
setosa
|
|
You can add row groups to ztable using addrgroup function. The n.rgroup means how much rows included in each row group. The cspan.rgroup means how much columns occupied by row group name.
rgroup=c("OneToThree","Four","FiveToSix")
n.rgroup=c(3,1,2)
z <- z %>%
addrgroup(rgroup=rgroup,n.rgroup=n.rgroup,cspan.rgroup=1)
z
|
Sepal
|
|
Petal
|
|
Species
|
|
Sepal.Length |
Sepal.Width |
|
Petal.Length |
Petal.Width |
|
Species |
OneToThree
|
|
|
|
|
|
|
|
1
|
5.10
|
3.50
|
|
1.40
|
0.20
|
|
setosa
|
2
|
4.90
|
3.00
|
|
1.40
|
0.20
|
|
setosa
|
3
|
4.70
|
3.20
|
|
1.30
|
0.20
|
|
setosa
|
Four
|
|
|
|
|
|
|
|
4
|
4.60
|
3.10
|
|
1.50
|
0.20
|
|
setosa
|
FiveToSix
|
|
|
|
|
|
|
|
5
|
5.00
|
3.60
|
|
1.40
|
0.20
|
|
setosa
|
6
|
5.40
|
3.90
|
|
1.70
|
0.40
|
|
setosa
|
|
You can add another colname(subcolname), the N count for example. The length of subcolnames should be same with column count of data.frame. You can use “NA” and the column name spans 2 rows.
ncount=c(123,120,123,124)
sub=paste("(N=",ncount,")",sep="")
z=addSubColNames(z,c(sub,NA))
z
|
Sepal
|
|
Petal
|
|
Species
|
|
Sepal.Length |
Sepal.Width |
|
Petal.Length |
Petal.Width |
|
Species |
|
(N=123)
|
(N=120)
|
|
(N=123)
|
(N=124)
|
|
OneToThree
|
|
|
|
|
|
|
|
1
|
5.10
|
3.50
|
|
1.40
|
0.20
|
|
setosa
|
2
|
4.90
|
3.00
|
|
1.40
|
0.20
|
|
setosa
|
3
|
4.70
|
3.20
|
|
1.30
|
0.20
|
|
setosa
|
Four
|
|
|
|
|
|
|
|
4
|
4.60
|
3.10
|
|
1.50
|
0.20
|
|
setosa
|
FiveToSix
|
|
|
|
|
|
|
|
5
|
5.00
|
3.60
|
|
1.40
|
0.20
|
|
setosa
|
6
|
5.40
|
3.90
|
|
1.70
|
0.40
|
|
setosa
|
|
You can merge cells by spanRow or spanCol function.
z=spanRow(z,col=2,from=4,to=7,bg="lightcyan",color="red")
z=spanRow(z,col=3,from=5,to=7,"platinum","blue")
z=spanRow(z,col=4,from=6,to=7,"cyan")
z=spanRow(z,col=5,from=5,to=7,"yellow")
z=spanRow(z,col=6,from=3,to=5,"yellow")
z
|
Sepal
|
|
Petal
|
|
Species
|
|
Sepal.Length |
Sepal.Width |
|
Petal.Length |
Petal.Width |
|
Species |
|
(N=123)
|
(N=120)
|
|
(N=123)
|
(N=124)
|
|
OneToThree
|
|
|
|
|
|
|
|
1
|
5.10
|
3.50
|
|
1.40
|
0.20
|
|
setosa
|
2
|
4.90
|
3.00
|
|
1.40
|
0.20
|
|
setosa
|
3
|
4.70
|
3.20
|
|
1.30
|
0.20
|
|
Four
|
|
|
|
|
|
4
|
3.10
|
|
1.50
|
0.20
|
|
FiveToSix
|
|
|
|
|
5
|
|
1.40
|
|
setosa
|
6
|
|
|
setosa
|
|
z=spanCol(z,row=2,from=3,to=4,"yellow")
z=spanCol(z,row=3,from=4,to=5,"lightblue")
z
|
Sepal
|
|
Petal
|
|
Species
|
|
Sepal.Length |
Sepal.Width |
|
Petal.Length |
Petal.Width |
|
Species |
|
(N=123)
|
(N=120)
|
|
(N=123)
|
(N=124)
|
|
OneToThree
|
|
|
|
|
|
|
|
1
|
5.10
|
3.50
|
0.20
|
|
setosa
|
2
|
4.90
|
3.00
|
|
1.40
|
|
setosa
|
3
|
4.70
|
3.20
|
|
1.30
|
0.20
|
|
Four
|
|
|
|
|
|
4
|
3.10
|
|
1.50
|
0.20
|
|
FiveToSix
|
|
|
|
|
5
|
|
1.40
|
|
setosa
|
6
|
|
|
setosa
|
|
You can add or adjust vertical lines of table by vlines function
vlines(z,type="all") # type=1 gets same result
|
Sepal
|
Petal
|
Species
|
|
Sepal.Length |
Sepal.Width |
Petal.Length |
Petal.Width |
Species |
|
(N=123)
|
(N=120)
|
(N=123)
|
(N=124)
|
OneToThree
|
|
|
|
|
|
1
|
5.10
|
3.50
|
0.20
|
setosa
|
2
|
4.90
|
3.00
|
1.40
|
setosa
|
3
|
4.70
|
3.20
|
1.30
|
0.20
|
Four
|
|
|
|
4
|
3.10
|
1.50
|
0.20
|
FiveToSix
|
|
|
5
|
1.40
|
setosa
|
6
|
setosa
|
|
z <- vlines(z,type="none") # type=0 gets same result
z
|
Sepal
|
|
Petal
|
|
Species
|
|
Sepal.Length |
Sepal.Width |
|
Petal.Length |
Petal.Width |
|
Species |
|
(N=123)
|
(N=120)
|
|
(N=123)
|
(N=124)
|
|
OneToThree
|
|
|
|
|
|
|
|
1
|
5.10
|
3.50
|
0.20
|
|
setosa
|
2
|
4.90
|
3.00
|
|
1.40
|
|
setosa
|
3
|
4.70
|
3.20
|
|
1.30
|
0.20
|
|
Four
|
|
|
|
|
|
4
|
3.10
|
|
1.50
|
0.20
|
|
FiveToSix
|
|
|
|
|
5
|
|
1.40
|
|
setosa
|
6
|
|
|
setosa
|
|
z <- z %>% vlines(add=c(1,2,5))
z
|
Sepal
|
|
Petal
|
|
Species
|
|
Sepal.Length |
Sepal.Width |
|
Petal.Length |
Petal.Width |
|
Species |
|
(N=123)
|
(N=120)
|
|
(N=123)
|
(N=124)
|
|
OneToThree
|
|
|
|
|
|
|
|
1
|
5.10
|
3.50
|
0.20
|
|
setosa
|
2
|
4.90
|
3.00
|
|
1.40
|
|
setosa
|
3
|
4.70
|
3.20
|
|
1.30
|
0.20
|
|
Four
|
|
|
|
|
|
4
|
3.10
|
|
1.50
|
0.20
|
|
FiveToSix
|
|
|
|
|
5
|
|
1.40
|
|
setosa
|
6
|
|
|
setosa
|
|
Please note that if you add vertical lines between groups, the space between groups(empty columns) disappeared and vice versa.
Merge two tables
You can bind two or more data.frame by cbind function.
t1=head(iris,10)[,c(1,3,5)]
t2=tail(iris,10)[,c(1,3,5)]
t=cbind(t1,t2)
z=ztable(t,caption="Table 1. Top 10 and Last 10 Data from iris",align="ccccccc")
z
Table 1. Top 10 and Last 10 Data from iris
|
Sepal.Length |
Petal.Length |
Species |
Sepal.Length |
Petal.Length |
Species |
1
|
5.10
|
1.40
|
setosa
|
6.70
|
5.60
|
virginica
|
2
|
4.90
|
1.40
|
setosa
|
6.90
|
5.10
|
virginica
|
3
|
4.70
|
1.30
|
setosa
|
5.80
|
5.10
|
virginica
|
4
|
4.60
|
1.50
|
setosa
|
6.80
|
5.90
|
virginica
|
5
|
5.00
|
1.40
|
setosa
|
6.70
|
5.70
|
virginica
|
6
|
5.40
|
1.70
|
setosa
|
6.70
|
5.20
|
virginica
|
7
|
4.60
|
1.40
|
setosa
|
6.30
|
5.00
|
virginica
|
8
|
5.00
|
1.50
|
setosa
|
6.50
|
5.20
|
virginica
|
9
|
4.40
|
1.40
|
setosa
|
6.20
|
5.40
|
virginica
|
10
|
4.90
|
1.50
|
setosa
|
5.90
|
5.10
|
virginica
|
|
And then, you can add column groups, row groups, add row colors, add column colors, add cell colors, and merge cells
cgroup=c("Top 10","Last 10")
n.cgroup=c(3,3)
z=addcgroup(z,cgroup=cgroup,n.cgroup=n.cgroup)
z
Table 1. Top 10 and Last 10 Data from iris
|
Top 10
|
|
Last 10
|
|
Sepal.Length |
Petal.Length |
Species |
|
Sepal.Length |
Petal.Length |
Species |
1
|
5.10
|
1.40
|
setosa
|
|
6.70
|
5.60
|
virginica
|
2
|
4.90
|
1.40
|
setosa
|
|
6.90
|
5.10
|
virginica
|
3
|
4.70
|
1.30
|
setosa
|
|
5.80
|
5.10
|
virginica
|
4
|
4.60
|
1.50
|
setosa
|
|
6.80
|
5.90
|
virginica
|
5
|
5.00
|
1.40
|
setosa
|
|
6.70
|
5.70
|
virginica
|
6
|
5.40
|
1.70
|
setosa
|
|
6.70
|
5.20
|
virginica
|
7
|
4.60
|
1.40
|
setosa
|
|
6.30
|
5.00
|
virginica
|
8
|
5.00
|
1.50
|
setosa
|
|
6.50
|
5.20
|
virginica
|
9
|
4.40
|
1.40
|
setosa
|
|
6.20
|
5.40
|
virginica
|
10
|
4.90
|
1.50
|
setosa
|
|
5.90
|
5.10
|
virginica
|
|
rgroup=c("Top 1-3","Top 4-5",NA," Top 7-10")
n.rgroup=c(3,2,1,4)
z=addrgroup(z,rgroup=rgroup,n.rgroup=n.rgroup,cspan.rgroup=1)
z
Table 1. Top 10 and Last 10 Data from iris
|
Top 10
|
|
Last 10
|
|
Sepal.Length |
Petal.Length |
Species |
|
Sepal.Length |
Petal.Length |
Species |
Top 1-3
|
|
|
|
|
|
|
|
1
|
5.10
|
1.40
|
setosa
|
|
6.70
|
5.60
|
virginica
|
2
|
4.90
|
1.40
|
setosa
|
|
6.90
|
5.10
|
virginica
|
3
|
4.70
|
1.30
|
setosa
|
|
5.80
|
5.10
|
virginica
|
Top 4-5
|
|
|
|
|
|
|
|
4
|
4.60
|
1.50
|
setosa
|
|
6.80
|
5.90
|
virginica
|
5
|
5.00
|
1.40
|
setosa
|
|
6.70
|
5.70
|
virginica
|
|
|
|
|
|
|
|
|
6
|
5.40
|
1.70
|
setosa
|
|
6.70
|
5.20
|
virginica
|
Top 7-10
|
|
|
|
|
|
|
|
7
|
4.60
|
1.40
|
setosa
|
|
6.30
|
5.00
|
virginica
|
8
|
5.00
|
1.50
|
setosa
|
|
6.50
|
5.20
|
virginica
|
9
|
4.40
|
1.40
|
setosa
|
|
6.20
|
5.40
|
virginica
|
10
|
4.90
|
1.50
|
setosa
|
|
5.90
|
5.10
|
virginica
|
|
z <- z %>%
addRowColor(c(5,10),"pink") %>%
addColColor(4,"amber") %>%
addCellColor(rows=c(5,10),cols=4,"red","white")
z
Table 1. Top 10 and Last 10 Data from iris
|
Top 10
|
|
Last 10
|
|
Sepal.Length |
Petal.Length |
Species |
|
Sepal.Length |
Petal.Length |
Species |
Top 1-3
|
|
|
|
|
|
|
|
1
|
5.10
|
1.40
|
setosa
|
|
6.70
|
5.60
|
virginica
|
2
|
4.90
|
1.40
|
setosa
|
|
6.90
|
5.10
|
virginica
|
3
|
4.70
|
1.30
|
setosa
|
|
5.80
|
5.10
|
virginica
|
Top 4-5
|
|
|
|
|
|
|
|
4
|
4.60
|
1.50
|
setosa
|
|
6.80
|
5.90
|
virginica
|
5
|
5.00
|
1.40
|
setosa
|
|
6.70
|
5.70
|
virginica
|
|
|
|
|
|
|
|
|
6
|
5.40
|
1.70
|
setosa
|
|
6.70
|
5.20
|
virginica
|
Top 7-10
|
|
|
|
|
|
|
|
7
|
4.60
|
1.40
|
setosa
|
|
6.30
|
5.00
|
virginica
|
8
|
5.00
|
1.50
|
setosa
|
|
6.50
|
5.20
|
virginica
|
9
|
4.40
|
1.40
|
setosa
|
|
6.20
|
5.40
|
virginica
|
10
|
4.90
|
1.50
|
setosa
|
|
5.90
|
5.10
|
virginica
|
|
z <- z %>%
spanCol(row=2,from=2,to=3,"lightcyan","red") %>%
spanRow(col=7,from=7,to=8,"cyan")
z
Table 1. Top 10 and Last 10 Data from iris
|
Top 10
|
|
Last 10
|
|
Sepal.Length |
Petal.Length |
Species |
|
Sepal.Length |
Petal.Length |
Species |
Top 1-3
|
|
|
|
|
|
|
|
1
|
5.10
|
setosa
|
|
6.70
|
5.60
|
virginica
|
2
|
4.90
|
1.40
|
setosa
|
|
6.90
|
5.10
|
virginica
|
3
|
4.70
|
1.30
|
setosa
|
|
5.80
|
5.10
|
virginica
|
Top 4-5
|
|
|
|
|
|
|
|
4
|
4.60
|
1.50
|
setosa
|
|
6.80
|
5.90
|
virginica
|
5
|
5.00
|
1.40
|
setosa
|
|
6.70
|
5.70
|
virginica
|
|
|
|
|
|
|
|
|
6
|
5.40
|
1.70
|
setosa
|
|
6.70
|
5.20
|
virginica
|
Top 7-10
|
|
|
|
|
|
|
7
|
4.60
|
1.40
|
setosa
|
|
6.30
|
5.00
|
8
|
5.00
|
1.50
|
setosa
|
|
6.50
|
5.20
|
virginica
|
9
|
4.40
|
1.40
|
setosa
|
|
6.20
|
5.40
|
virginica
|
10
|
4.90
|
1.50
|
setosa
|
|
5.90
|
5.10
|
virginica
|
|
Table 1. Top 10 and Last 10 Data from iris
|
Top 10
|
|
Last 10
|
|
Sepal.Length |
Petal.Length |
Species |
|
Sepal.Length |
Petal.Length |
Species |
Top 1-3
|
|
|
|
|
|
|
|
1
|
5.10
|
setosa
|
|
6.70
|
5.60
|
virginica
|
2
|
4.90
|
1.40
|
setosa
|
|
6.90
|
5.10
|
virginica
|
3
|
4.70
|
1.30
|
setosa
|
|
5.80
|
5.10
|
virginica
|
Top 4-5
|
|
|
|
|
|
|
|
4
|
4.60
|
1.50
|
setosa
|
|
6.80
|
5.90
|
virginica
|
5
|
5.00
|
1.40
|
setosa
|
|
6.70
|
5.70
|
virginica
|
|
|
|
|
|
|
|
|
6
|
5.40
|
1.70
|
setosa
|
|
6.70
|
5.20
|
virginica
|
Top 7-10
|
|
|
|
|
|
|
7
|
4.60
|
1.40
|
setosa
|
|
6.30
|
5.00
|
8
|
5.00
|
1.50
|
setosa
|
|
6.50
|
5.20
|
virginica
|
9
|
4.40
|
1.40
|
setosa
|
|
6.20
|
5.40
|
virginica
|
10
|
4.90
|
1.50
|
setosa
|
|
5.90
|
5.10
|
virginica
|
|
And you can adjust vertical lines, too.
vlines(z,type=0) # No vertical lines
Table 1. Top 10 and Last 10 Data from iris
|
Top 10
|
|
Last 10
|
|
Sepal.Length |
Petal.Length |
Species |
|
Sepal.Length |
Petal.Length |
Species |
Top 1-3
|
|
|
|
|
|
|
|
1
|
5.10
|
setosa
|
|
6.70
|
5.60
|
virginica
|
2
|
4.90
|
1.40
|
setosa
|
|
6.90
|
5.10
|
virginica
|
3
|
4.70
|
1.30
|
setosa
|
|
5.80
|
5.10
|
virginica
|
Top 4-5
|
|
|
|
|
|
|
|
4
|
4.60
|
1.50
|
setosa
|
|
6.80
|
5.90
|
virginica
|
5
|
5.00
|
1.40
|
setosa
|
|
6.70
|
5.70
|
virginica
|
|
|
|
|
|
|
|
|
6
|
5.40
|
1.70
|
setosa
|
|
6.70
|
5.20
|
virginica
|
Top 7-10
|
|
|
|
|
|
|
7
|
4.60
|
1.40
|
setosa
|
|
6.30
|
5.00
|
8
|
5.00
|
1.50
|
setosa
|
|
6.50
|
5.20
|
virginica
|
9
|
4.40
|
1.40
|
setosa
|
|
6.20
|
5.40
|
virginica
|
10
|
4.90
|
1.50
|
setosa
|
|
5.90
|
5.10
|
virginica
|
|
vlines(z,type=1) # Vertical lines for all column
Table 1. Top 10 and Last 10 Data from iris
|
Top 10
|
Last 10
|
|
Sepal.Length |
Petal.Length |
Species |
Sepal.Length |
Petal.Length |
Species |
Top 1-3
|
|
|
|
|
|
|
1
|
5.10
|
setosa
|
6.70
|
5.60
|
virginica
|
2
|
4.90
|
1.40
|
setosa
|
6.90
|
5.10
|
virginica
|
3
|
4.70
|
1.30
|
setosa
|
5.80
|
5.10
|
virginica
|
Top 4-5
|
|
|
|
|
|
|
4
|
4.60
|
1.50
|
setosa
|
6.80
|
5.90
|
virginica
|
5
|
5.00
|
1.40
|
setosa
|
6.70
|
5.70
|
virginica
|
|
|
|
|
|
|
|
6
|
5.40
|
1.70
|
setosa
|
6.70
|
5.20
|
virginica
|
Top 7-10
|
|
|
|
|
|
7
|
4.60
|
1.40
|
setosa
|
6.30
|
5.00
|
8
|
5.00
|
1.50
|
setosa
|
6.50
|
5.20
|
virginica
|
9
|
4.40
|
1.40
|
setosa
|
6.20
|
5.40
|
virginica
|
10
|
4.90
|
1.50
|
setosa
|
5.90
|
5.10
|
virginica
|
|