Convert vectors or square numeric matrices to and from string representation
Source:R/string_representation.R
string_representation.Rd
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]").
Usage
mat_to_string(mat)
vec_to_string(vec)
string_to_mat(mat_str)
string_to_vec(vec_str, numeric = FALSE)
Value
A square numeric matrix (string_to_mat
), vector
(string_to_vec
), or string (mat_to_string
or
vec_to_string
).
See also
Other data management:
cdb_flatten()
,
cdb_id_stages()
,
cdb_id_studies()
,
cdb_id()
,
cdb_mean_matF()
,
cdb_rbind()
,
cdb_unflatten()
,
cdb_unnest()
,
mpm_elementwise_apply()
,
mpm_mean()
,
mpm_median()
,
mpm_sd()
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]"
if (FALSE) { # \dontrun{
# non-square matrix
mat_str <- "[0.42 0.52 0.15 0.23 0.14]"
string_to_mat(mat_str)
} # }