Skip to contents

Converts synchronicity matrices (from synch_pair_analysis() or synch_neighbor_analysis()) to a tidy data frame format with one row per animal pair. Extracts values from the upper triangle of the matrices and organizes them into a data frame suitable for further analysis or visualization.

Usage

synch_pairs_to_df(
  synch_results,
  min_time = 0,
  sort_by = "total_time",
  decreasing = TRUE
)

Arguments

synch_results

List output from synch_pair_analysis() or synch_neighbor_analysis(), containing bout, total_time, and avg_duration elements (matrices or lists of matrices)

min_time

Numeric, minimum total_time threshold to include a pair (default 0, which includes all pairs with any activity). Use this to filter out pairs with negligible interaction.

sort_by

Character, column name to sort by. Common options: "total_time" (default), "bouts", "avg_duration", or NULL for no sorting.

decreasing

Logical, if TRUE (default) sorts in descending order, if FALSE sorts ascending

Value

Data frame with columns:

  • animal1: Character, first animal ID in pair

  • animal2: Character, second animal ID in pair

  • bouts: Integer, number of distinct bouts together

  • total_time: Numeric, total time together (seconds or minutes)

  • avg_duration: Numeric, average duration per bout

  • day: Character, day identifier (only for multi-day input)

Only includes pairs where total_time > min_time. Sorted by sort_by column if specified.

Examples

# Create toy data
toy_data <- data.frame(
  animal = c(1, 2, 3, 1, 2, 3),
  start = lubridate::ymd_hms(c(
    "2023-01-01 10:00:00", "2023-01-01 10:00:01",
    "2023-01-01 10:00:05", "2023-01-01 10:00:10",
    "2023-01-01 10:00:11", "2023-01-01 10:00:12"
  )),
  end = lubridate::ymd_hms(c(
    "2023-01-01 10:00:03", "2023-01-01 10:00:04",
    "2023-01-01 10:00:08", "2023-01-01 10:00:13",
    "2023-01-01 10:00:14", "2023-01-01 10:00:15"
  )),
  bin = c(1, 2, 3, 1, 2, 3),
  start_weight = c(10.5, 8.3, 9.1, 10.2, 8.0, 7.5),
  end_weight = c(10.2, 8.1, 8.9, 9.9, 7.8, 7.3)
)

# Process matrices
matrices <- matrix_process(toy_data, type = "feed",
                          id_col = "animal", start_col = "start",
                          end_col = "end", bin_col = "bin",
                          start_weight_col = "start_weight",
                          end_weight_col = "end_weight",
                          bins_feed = 1:3)

# Analyze pairs
pair_results <- synch_pair_analysis(matrices, type = "feed",
                                   id_col = "animal")

# Convert to data frame
pair_df <- synch_pairs_to_df(pair_results)
print(pair_df)
#>   animal1 animal2 bouts total_time avg_duration
#> 1       1       2     2          6            3
#> 2       2       3     1          3            3
#> 3       1       3     1          2            2