Compact Representation of Binary, 0/1 Values
compact01.RdOf 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.
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)).
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()))))