如何找到 events/letters/ 出现的模式?

How to find the pattern of occurrences of events/letters/?

我有一个事件数据集(在这种情况下是字母),我想找出哪些字母触发了哪些字母的出现(例如,字母 c 总是第一个然后 d 然后 i)。换句话说,检查这些字母的出现是否存在模式以及模式是什么。

set.seed(123) df <- data.frame(x = sample(letters[1:6], 500, replace=TRUE))

根据您的示例代码,您可以尝试

freqtab <- table(df$x[-length(df$x)], df$x[-1])

这会给你完整的(前面的字母是行,后面的字母是列)

freqtab

#     a  b  c  d  e  f
#  a 13 13 19 11 12 13
#  b 17 16 17 10 17 15
#  c 13 16 18 14 17 14
#  d  8 17 16  9  9 13
#  e 20 13 10 13 15 11
#  f 10 16 12 15 13 14

如果你想得到一个特定的行,比如 c 后面字母的频率,你可以使用

freqtab["c", ]
#  a  b  c  d  e  f 
# 13 16 18 14 17 14

还有很多其他方法可以解决这个问题