Function to print a matrix with stacked cells
print.char.matrix.RdPrints a dataframe or matrix in stacked cells. Line break charcters in a matrix element will result in a line break in that cell, but tab characters are not supported.
Usage
# S3 method for class 'char.matrix'
print(x, file = "", col.name.align = "cen", col.txt.align = "right",
cell.align = "cen", hsep = "|", vsep = "-", csep = "+", row.names = TRUE,
col.names = FALSE, append = FALSE,
top.border = TRUE, left.border = TRUE, ...)Arguments
- x
a matrix or dataframe
- file
name of file if file output is desired. If left empty, output will be to the screen
- col.name.align
if column names are used, they can be aligned right, left or centre. Default
"cen"results in names centred between the sides of the columns they name. If the width of the text in the columns is less than the width of the name,col.name.alignwill have no effect. Other options are"right"and"left".- col.txt.align
how character columns are aligned. Options are the same as for
col.name.alignwith no effect when the width of the column is greater than its name.- cell.align
how numbers are displayed in columns
- hsep
character string to use as horizontal separator, i.e. what separates columns
- vsep
character string to use as vertical separator, i.e. what separates rows. Length cannot be more than one.
- csep
character string to use where vertical and horizontal separators cross. If
hsepis more than one character,csepwill need to be the same length. There is no provision for multiple vertical separators- row.names
logical: are we printing the names of the rows?
- col.names
logical: are we printing the names of the columns?
- append
logical: if
fileis not"", are we appending to the file or overwriting?- top.border
logical: do we want a border along the top above the columns?
- left.border
logical: do we want a border along the left of the first column?
- ...
unused
Details
If any column of x is a mixture of character and numeric, the
distinction between character and numeric columns will be lost. This
is especially so if the matrix is of a form where you would not want
to print the column names, the column information being in the rows at
the beginning of the matrix.
Row names, if not specified in the making of the matrix will simply be
numbers. To prevent printing them, set row.names = FALSE.
Author
Patrick Connolly p.connolly@hortresearch.co.nz
Examples
data(HairEyeColor)
print.char.matrix(HairEyeColor[ , , "Male"], col.names = TRUE)
#> +-----+-----+----+-----+-----+
#> | Hair|Brown|Blue|Hazel|Green|
#> +-----+-----+----+-----+-----+
#> |Black| 32 | 11 | 10 | 3 |
#> +-----+-----+----+-----+-----+
#> |Brown| 53 | 50 | 25 | 15 |
#> +-----+-----+----+-----+-----+
#> | Red| 10 | 10 | 7 | 7 |
#> +-----+-----+----+-----+-----+
#> |Blond| 3 | 30 | 5 | 8 |
#> +-----+-----+----+-----+-----+
print.char.matrix(HairEyeColor[ , , "Female"], col.txt.align = "left", col.names = TRUE)
#> +-----+-----+----+-----+-----+
#> | Hair|Brown|Blue|Hazel|Green|
#> +-----+-----+----+-----+-----+
#> |Black| 36 | 9 | 5 | 2 |
#> +-----+-----+----+-----+-----+
#> |Brown| 66 | 34 | 29 | 14 |
#> +-----+-----+----+-----+-----+
#> |Red | 16 | 7 | 7 | 7 |
#> +-----+-----+----+-----+-----+
#> |Blond| 4 | 64 | 5 | 8 |
#> +-----+-----+----+-----+-----+
z <- rbind(c("", "N", "y"),
c("[ 1.34,40.3)\n[40.30,48.5)\n[48.49,58.4)\n[58.44,87.8]",
" 50\n 50\n 50\n 50",
"0.530\n0.489\n0.514\n0.507"),
c("female\nmale", " 94\n106", "0.552\n0.473" ),
c("", "200", "0.510"))
dimnames(z) <- list(c("", "age", "sex", "Overall"),NULL)
print.char.matrix(z)
#> +-------+------------+---+-----+
#> | | | N| y|
#> +-------+------------+---+-----+
#> | age|[ 1.34,40.3)| 50|0.530|
#> | |[40.30,48.5)| 50|0.489|
#> | |[48.49,58.4)| 50|0.514|
#> | |[58.44,87.8]| 50|0.507|
#> +-------+------------+---+-----+
#> | sex| female| 94|0.552|
#> | | male|106|0.473|
#> +-------+------------+---+-----+
#> |Overall| |200|0.510|
#> +-------+------------+---+-----+