Reputation: 21
I am currently working on a dataset for animal behavior, and I want to create a function that can analyze one vector containing different postures and give the output as a primary transition matrix. Let's call the postures A, B, and C, and the result is a primary transition matrix (i.e., how frequently A is followed by B, A is followed by C, B is followed by A, etc.)
I am relatively new to this type of analysis, and I don't know how to approach this. Any input is highly appreciated!
Assuming my input data is this:
df <- data.frame(c("A", "B", "A", "B", "C", "B", "C", "A", "C", "B", "A", "B"))
The desired output (as a matrix) will look like this:
A | B | C | |
---|---|---|---|
A | 0 | 3 | 1 |
B | 2 | 0 | 3 |
C | 1 | 2 | 0 |
This matrix shows the number of each type of transition for the posture given to the left (1st column) in df.
Once more, I'm not sure how to tackle this because I'm pretty new to this kind of analysis. I would greatly appreciate any feedback!
Upvotes: 2
Views: 58
Reputation: 102710
You could use table
> t(table(as.data.frame(embed(unlist(df), 2))))
V1
V2 A B C
A 0 3 1
B 2 0 2
C 1 2 0
or
> t(table(asplit(embed(unlist(df), 2), 2)))
.1
.2 A B C
A 0 3 1
B 2 0 2
C 1 2 0
Upvotes: 4
Reputation: 19484
Perhaps create leads and then table:
library(dplyr)
mutate(df, x2=lead(x1)) |>
table()
x2
x1 A B C
A 0 3 1
B 2 0 2
C 1 2 0
Or simply:
table(df[1:(nrow(df)-1),], df[2:nrow(df),])
df <- data.frame(x1=c("A", "B", "A", "B", "C", "B", "C", "A", "C", "B", "A", "B"))
Upvotes: 3