rep_char: Repeat a character a variable number of times

Description Usage Arguments Value Author(s) Examples

Description

Effectively a version of rep, where only once value can be repeated (by default, a space; " "), but it can be repeated a variable number of times. Useful for creating even spacing for print and summary methods.

Usage

1
rep_char(x = " ", times)

Arguments

x

A value to repeat. Will be coerced to character.

times

A numeric vector; the number of times that x should be repeated.

Value

A character vector of x repated various times

Author(s)

Brendan Rocks rocks.brendan@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Strings repeating 'a' a variable number of times!
rep_char("a", 1:5)

# Slightly more useful. Some text strings which we'd like to present:
desc <- c("\n", "first : 1st\n", "second : 2nd\n", "third : 3rd\n",
"fourth : 4th\n", "umpteenth : ...\n")

# However, the varying lengths make them look a little awkward
cat(desc)

# We can use rep_char to add extra spaces to the strings which are shorter
# than the longest
desc_spaced <- paste0(rep_char(times = max(nchar(desc)) - nchar(desc)), desc)

# Much better
cat(desc_spaced)

brendan-r/brocks documentation built on May 13, 2019, 5:08 a.m.