Fills a string vector with a user-provided symbol, up to the required length.

sfill(x = "", n = NULL, symbol = " ", right = FALSE, anchor, na = "NA")

Arguments

x

A character vector.

n

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

symbol

Character scalar, default to " ". The symbol used to fill.

right

Logical, default is FALSE. Whether the character vector should be filled on the left( default) or on the right.

anchor

Character scalar, can be missing. If provided, the filling is done up to this anchor. See examples.

na

Character that will replace any NA value in input. Default is "NA".

Value

Returns a character vector of the same length as x.

Examples


# 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