Functions to convert vectors or square numeric matrices to and from string representation, which is primarily useful for writing data frames with list-columns containing vectors or matrices to a flat file format such as csv.

String representations of vectors and matrices begin with an open bracket ("[") and end with a closed bracket ("]"). Matrix elements are separated with a space ("[0.2 0.3 0.1 0]") whereas vector elements are separate with two vertical bars ("[Seedling||Juvenile||Reproductive]").

mat_to_string(mat)

vec_to_string(vec)

string_to_mat(mat_str)

string_to_vec(vec_str, numeric = FALSE)

Arguments

mat

A square numeric matrix

vec

A vector

mat_str

A square numeric matrix in string representation

vec_str

A vector in string representation

numeric

Logical value indicating whether a string representation of a vector should be coerced to numeric (if FALSE remains character)

Value

A square numeric matrix (string_to_mat), vector (string_to_vec), or string (mat_to_string or vec_to_string).

See also

Examples

mat_str <- "[3.3 5.2 6.1 0.1 NA 0.3 0.2 0.4 0.1]" mat <- string_to_mat(mat_str) vec1_str <- "[0.30||0.42||0.19||0.09]" vec1 <- string_to_vec(vec1_str, numeric = TRUE) vec2_str <- "[Seedling 1||Seedling 2||Juvenile||Reproductive]" vec2 <- string_to_vec(vec2_str) # convert back to string format mat_to_string(mat)
#> [1] "[3.3 5.2 6.1 0.1 NA 0.3 0.2 0.4 0.1]"
vec_to_string(vec1)
#> [1] "[0.3||0.42||0.19||0.09]"
vec_to_string(vec2)
#> [1] "[Seedling 1||Seedling 2||Juvenile||Reproductive]"
# NOT RUN { # non-square matrix mat_str <- "[0.42 0.52 0.15 0.23 0.14]" string_to_mat(mat_str) # }