pillar icon indicating copy to clipboard operation
pillar copied to clipboard

Add an option for controlling the zero, NA characters

Open richierocks opened this issue 6 years ago • 1 comments

If you have data with many zeroes, it can be useful to replace the 0 character with something subtler in printed output. For example, the Matrix package defaults to printing zeroes as a . for sparse matrices. Similarly, prettyNum() lets you override the zero character.

set.seed(1979)
# Sparse matrices print with . instead of 0 
library(Matrix)
spMatrix(10, 10, sample(10, 3), sample(10, 3), 1:3)
#> 10 x 10 sparse Matrix of class "dgTMatrix"
#>                          
#>  [1,] . . . . . . . . . .
#>  [2,] . . . . . . . . . .
#>  [3,] . . 3 . . . . . . .
#>  [4,] . . . . . . . . . .
#>  [5,] . 2 . . . . . . . .
#>  [6,] . . . . . . . . . .
#>  [7,] . . . . . . . . . .
#>  [8,] . . . . . . . . . .
#>  [9,] . . . . . . . . . .
#> [10,] . . . . . . . 1 . .

# You can specify this for vectors using prettyNum()
(pois <- rpois(100, 0.1))
#>   [1] 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0
#>  [36] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
#>  [71] 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
prettyNum(pois, zero.print = ".")
#>   [1] "." "." "." "." "." "." "1" "." "." "." "." "." "." "." "1" "." "."
#>  [18] "1" "." "." "." "." "." "1" "." "." "." "1" "." "." "1" "." "." "."
#>  [35] "." "." "." "." "." "." "." "." "." "." "." "." "." "." "." "." "."
#>  [52] "." "." "." "." "." "." "." "." "1" "." "." "." "." "." "." "." "."
#>  [69] "." "." "." "." "." "." "." "." "1" "." "." "." "." "." "." "." "."
#>  [86] "." "." "." "." "." "." "." "." "." "." "." "." "." "." "."

It seems useful to have a pillar option to allow overriding the zero character. Similarly, if data has a lot of missing values, it seems useful to have an option to replace NA with another subtler character.

richierocks avatar Mar 06 '19 22:03 richierocks

Thanks. I wonder if this could be an option in num()?

krlmlr avatar Jun 30 '22 02:06 krlmlr