View source: R/stri_join_mat.R
stri_join_mat | R Documentation |
The stri_join_mat()
function
(and their aliases stri_c_mat
and stri_paste_mat
)
perform row-wise (margin = 1
; the default) or
column-wise (margin = 2
) joining of a matrix of strings,
thereby transforming a matrix of strings into a vector of strings.
stri_join_mat(mat, margin = 1, sep = "", collapse = NULL)
stri_c_mat(mat, margin = 1, sep = "", collapse = NULL)
stri_paste_mat(mat, margin = 1, sep = "", collapse = NULL)
mat |
a matrix of strings |
margin |
the margin over which the strings must be joined.
|
sep , collapse |
as in stri_join. |
The stri_join_mat()
function, and its aliases, return a vector of strings.
tinycodet_strings
#############################################################################
# Basic example
x <- matrix(letters[1:25], ncol = 5, byrow = TRUE)
print(x)
stri_join_mat(x, margin = 1)
x <- matrix(letters[1:25], ncol = 5, byrow = FALSE)
print(x)
stri_join_mat(x, margin = 2)
#############################################################################
# sorting characters in strings ====
x <- c(paste(sample(letters), collapse = ""),
paste(sample(letters), collapse = ""))
print(x)
mat <- strcut_brk(x)
rank <- stringi::stri_rank(as.vector(mat)) |> matrix(ncol = ncol(mat))
sorted <- mat %row~% rank
sorted[is.na(sorted)] <- ""
print(sorted)
stri_join_mat(sorted, margin = 1)
stri_join_mat(sorted, margin = 2)
#############################################################################
# sorting words ====
x <- c("2nd 3rd 1st", "Goodbye everyone")
print(x)
mat <- strcut_brk(x, "word")
rank <- stringi::stri_rank(as.vector(mat)) |> matrix(ncol = ncol(mat))
sorted <- mat %row~% rank
sorted[is.na(sorted)] <- ""
stri_c_mat(sorted, margin = 1, sep = " ") # <- alias for stri_join_mat
stri_c_mat(sorted, margin = 2, sep = " ")
#############################################################################
# randomly shuffling sentences ====
x <- c("Hello, who are you? Oh, really?! Cool!",
"I don't care. But I really don't.")
print(x)
mat <- strcut_brk(x, "sentence")
rank <- sample(seq_along(mat)) |> matrix(ncol = ncol(mat))
sorted <- mat %row~% rank
sorted[is.na(sorted)] <- ""
stri_paste_mat(sorted, margin = 1) # <- another alias for stri_join_mat
stri_paste_mat(sorted, margin = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.