| strings | R Documentation |
Fast string concatenation using C++
paste_(..., sep = "", collapse = NULL, .args = NULL)
... |
Character vectors to concatenate. |
sep |
|
collapse |
Optional string to collapse concatenated strings into one string (character vector of length 1). |
.args |
An alternative to |
library(cheapr)
# Normal usage
paste_("Hello", "and", "Goodbye", sep = " ")
paste_(100, "%")
paste_(letters, LETTERS)
paste_(letters, LETTERS, collapse = "")
# Both concatenating and collapsing
paste_(letters, LETTERS, sep = ",", collapse = " next letter ")
# This is the same as above
paste_(letters, LETTERS, collapse = ", next letter ")
# Recycling with zero-length vectors
paste_("hello", character(), letters)
paste_("hello", character(), letters, collapse = "")
library(bench)
sampled_letters <- sample_(letters, 5e04, TRUE)
# Pasting multiple character vectors
mark(
paste_(sampled_letters, sep = ","),
paste(sampled_letters, sep = ","),
iterations = 50
)
# Collapsing is very fast compared to base R
mark(
paste_(sampled_letters, collapse = ""),
paste(sampled_letters, collapse = ""),
iterations = 50
)
# Concatenating many objects is very fast via `.args`
strings <- sampled_letters |>
with_local_seed(1) |>
as.list()
strings <- lapply(strings, rep_len_, 3)
mark(
paste_(.args = strings),
do.call(paste0, strings),
iterations = 15
)
mark(
paste_(.args = strings, collapse = ""),
do.call(paste_, c(strings, list(collapse = ""))),
do.call(paste0, c(strings, list(collapse = ""))),
iterations = 10
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.