Skip to contents

Of a 0-1 i.e., binary vector, using symnum(z == 1), produce a compact “visual” character representation for human consumption.

No rocket science, but a nice example of combining basic R idioms.

Usage

compact01(z, ncol = 50L)

Arguments

z

a vector, typically of 0s and 1s, but more generally of 1s and “everything else” which you'd want to show visually. Inside compact01(z, *), only z == 1 is used.

ncol

a positive integer, specifying the number of “columns” of the result.

Value

a character matrix Ch, \(K \times 1\), where each row is a string of "." and "|" of string length typically ncol, but for short z, will have nchar(Ch[1,1]) == min(ncol, length(z)).

Author

Martin Maechler

See also

Examples

set.seed(12)
z <- rbinom(120, size = 1, prob = 1/4)
(compact01(z) -> Ch)
#>                                                       
#>   1 .||........|.........|.........|||.|.|....|.|....|
#>  51 .....|...|..|....|.|.....||..|.|.....|........|...
#> 101 .|.|..|...|........|                              
 compact01(z, 20)
#>                         
#>   1 .||........|........
#>  21 .|.........|||.|.|..
#>  41 ..|.|....|.....|...|
#>  61 ..|....|.|.....||..|
#>  81 .|.....|........|...
#> 101 .|.|..|...|........|
## "visualize"  a TRUE/FALSE property
x <- rnorm(150)
compact01(abs(x) > 1.5)
#>                                                       
#>   1 ......||................................|.|......|
#>  51 .................||.................|..|.......|..
#> 101 |.|....|..........|.|...........|.......|....|....
stopifnot(identical(0:1, dim(compact01(integer()))))