Learn R Programming

AnimalSequences (version 0.2.0)

calculate_transition_probs: Calculate Transition Probabilities from Sequences

Description

This function calculates the transition probabilities between elements in a set of sequences. It computes the probability of transitioning from one element to another based on the frequency of transitions observed in the input sequences.

Usage

calculate_transition_probs(sequences)

Value

A data frame with the following columns:

previous_element

The element that transitions to the next element.

element

The element that follows the previous element.

count

The number of times the transition from the previous element to the current element occurs.

probability

The probability of transitioning from the previous element to the current element.

Arguments

sequences

A vector of character strings, where each string represents a sequence of elements separated by spaces.

Details

The function uses the `unnest_tokens` function from the `tidytext` package to split sequences into individual elements. It then calculates transition counts and probabilities for each pair of consecutive elements in the sequences. The resulting data frame shows the transition probabilities for each possible element pair.

Examples

Run this code
library(tidytext)
sequences <- c("A B C", "A B", "B C A")
calculate_transition_probs(sequences)

Run the code above in your browser using DataLab