Fills a string vector with a user-provided symbol, up to the required length.
sfill(x = "", n = NULL, symbol = " ", right = FALSE, anchor, na = "NA")
A character vector.
A positive integer giving the total expected length of each character string. Can be NULL (default). If NULL
, then n
is set to the maximum number of characters in x
(i.e. max(nchar(x))
).
Character scalar, default to " "
. The symbol used to fill.
Logical, default is FALSE
. Whether the character vector should be filled on the left( default) or on the right.
Character scalar, can be missing. If provided, the filling is done up to this anchor. See examples.
Character that will replace any NA value in input. Default is "NA".
Returns a character vector of the same length as x
.
# Some self-explaining examples
x = c("hello", "I", "am", "No-one")
cat(sep = "\n", sfill(x))
#> hello
#> I
#> am
#> No-one
cat(sep = "\n", sfill(x, symbol = "."))
#> .hello
#> .....I
#> ....am
#> No-one
cat(sep = "\n", sfill(x, symbol = ".", n = 15))
#> ..........hello
#> ..............I
#> .............am
#> .........No-one
cat(sep = "\n", sfill(x, symbol = ".", right = TRUE))
#> hello.
#> I.....
#> am....
#> No-one
cat(sep = "\n", paste(sfill(x, symbol = ".", right = TRUE), ":", 1:4))
#> hello. : 1
#> I..... : 2
#> am.... : 3
#> No-one : 4
# Argument 'anchor' can be useful when using numeric vectors
x = c(-15.5, 1253, 32.52, 665.542)
cat(sep = "\n", sfill(x))
#> -15.5
#> 1253
#> 32.52
#> 665.542
cat(sep = "\n", sfill(x, anchor = "."))
#> -15.5
#> 1253
#> 32.52
#> 665.542