string_representation: Convert vectors or square numeric matrices to and from string...

string_representationR Documentation

Convert vectors or square numeric matrices to and from string representation

Description

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)

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).

Author(s)

Owen R. Jones <jones@biology.sdu.dk>

Patrick M. Barks <patrick.barks@gmail.com>

See Also

cdb_flatten cdb_unflatten

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)
vec_to_string(vec1)
vec_to_string(vec2)

## Not run: 
# non-square matrix
mat_str <- "[0.42 0.52 0.15 0.23 0.14]"
string_to_mat(mat_str)

## End(Not run)

Rcompadre documentation built on Oct. 17, 2024, 1:07 a.m.