Skip to contents

This function creates a contingency table from a two- or three-columns data.frame where each row represents the interaction between two nodes (site and species for example) and an optional third column indicating the weight of the interaction (if weight = TRUE).

Usage

net_to_mat(
  net,
  weight = FALSE,
  squared = FALSE,
  symmetrical = FALSE,
  missing_value = 0
)

Arguments

net

a two- or three-columns data.frame where each row represents the interaction between two nodes (site and species for example) and an optional third column indicating the weight of the interaction.

weight

a boolean indicating if the weight should be considered

squared

a boolean indicating if the output matrix should but squared (same nodes in rows and columns).

symmetrical

a boolean indicating if the resulting matrix should be symmetrical (only if squared = TRUE). Note that different weights associated with two opposite pairs already present in net will be preserved.

missing_value

the value to assign to the pairs of nodes not present in net (0 by default).

Value

A matrix with the first nodes (first column of net) as rows and the second nodes (second column of net) as columns. Note that if squared = TRUE the rows and columns have the same number of elements corresponding to the concatenation of unique objects in net's first and second columns. If squared = TRUE the matrix can be forced to be symmetrical based on the upper triangular part of the matrix.

See also

Author

Maxime Lenormand (maxime.lenormand@inrae.fr), Pierre Denelle (pierre.denelle@gmail.com) and Boris Leroy (leroy.boris@gmail.com)

Examples

net <- data.frame(
  Site = c(rep("A", 2), rep("B", 3), rep("C", 2)),
  Species = c("a", "b", "a", "c", "d", "b", "d"),
  Weight = c(10, 100, 1, 20, 50, 10, 20)
)

mat <- net_to_mat(net, weight = TRUE)