stri_join_mat {tinycodet}R Documentation

Concatenate Character Matrix Row-wise or Column-wise

Description

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.

Usage

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)

Arguments

mat

a matrix of strings

margin

the margin over which the strings must be joined.

  • If margin = 1, the elements within each row of matrix mat are joined into a single string. Thus if the matrix has 10 rows, it returns a vector of 10 strings.

  • If margin = 2, the elements within each column of matrix mat are joined into a single string. Thus if the matrix has 10 columns, it returns a vector of 10 strings.

sep, collapse

as in stri_join.

Value

The stri_join_mat() function, and its aliases, return a vector of strings.

See Also

tinycodet_strings

Examples


#############################################################################

# 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(1:length(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)


[Package tinycodet version 0.5.0 Index]