Learn R Programming

sfsmisc (version 1.1-25)

compact01: Compact Representation of Binary, 0/1 Values

Description

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)

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

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.

Author

Martin Maechler

See Also

Examples

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

Run the code above in your browser using DataLab