Concatenate a number of copies of each string.
strrep(x, times)
e1 %x*% e2
| | |
|----|----|
| e1
, x
| character vector (or an object coercible to) whose elements are to be duplicated |
| e2
, times
| numeric vector giving the number of times to repeat the corresponding strings |
Both arguments are recycled if necessary.
The `%x*%`
operator mimics a vectorised version of Python\'s `*`
for strings (str.__mul__
).
A character vector (in UTF-8).
`%x*%`
and strrep
preserve object attributes in a similar way as other Arithmetic operators.
Replacement for base strrep
implemented with stri_dup
.
partial recycling with no warning \"longer object length is not a multiple of shorter object length\" [fixed here]
base strrep
seems to preserve only the names
attribute, and only if the input is of type character (whilst paste
preserves nothing) [fixed]
overloading `*.character`
has no effect in R, because S3 method dispatch is done internally with hard-coded support for character arguments. We could have replaced the generic `*`
with the one that calls UseMethod
, but it feels like a too intrusive solution [fixed by introducing `%x+%`
operator]
The official online manual of stringx at https://stringx.gagolewski.com/
Related function(s): paste
, sprintf
x <- structure(c(A="a", B=NA, C="c"), attrib1="value1")
x %x*% 3
## A B C
## "aaa" NA "ccc"
## attr(,"attrib1")
## [1] "value1"
x %x*% 1:3
## A B C
## "a" NA "ccc"
## attr(,"attrib1")
## [1] "value1"
"a" %x*% 1:3
## [1] "a" "aa" "aaa"
stringx::strrep(x, 3)
## A B C
## "aaa" NA "ccc"
## attr(,"attrib1")
## [1] "value1"
base::strrep(x, 3)
## A B C
## "aaa" NA "ccc"
y <- matrix(1:6, nrow=2, dimnames=list(c("A", "B"), NULL))
y %x*% 1:2
## [1] "1" "22" "3" "44" "5" "66"
stringx::strrep(y, 1:2)
## [1] "1" "22" "3" "44" "5" "66"
base::strrep(y, 1:2)
## [1] "1" "22" "3" "44" "5" "66"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.