
Convert synchronicity matrices to pair-level data frame
Source:R/synch_pairs_to_df.R
synch_pairs_to_df.RdConverts 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.
Arguments
- synch_results
List output from
synch_pair_analysis()orsynch_neighbor_analysis(), containingbout,total_time, andavg_durationelements (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 pairanimal2: Character, second animal ID in pairbouts: Integer, number of distinct bouts togethertotal_time: Numeric, total time together (seconds or minutes)avg_duration: Numeric, average duration per boutday: 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