R arulesSequences Find which patterns are supported by a sequence

情到浓时终转凉″ 提交于 2019-12-06 05:02:26

After some cool-headed digging, I found a way to do it, and indeed, it was easy... since the support function does the job!

ids <- unique(zaki@itemsetInfo$sequenceID)
encoding <- data.frame()

# Prepare the data.frame: as many columns as there are frequent sequences
for (seq_id in 1:length(frequent_sequences)){
    encoding[,labels(frequent_sequences[seq_id])] <- logical(0)
}

# Fill the rows
for (id in ids){
    transaction_subset <- zaki[zaki@itemsetInfo$sequenceID==id]
    encoding[id, ] <- as.logical(
        support(frequent_sequences, transaction_subset, type="absolute")
        )
}

There might be more aesthetic ways to reach the result, but this yields the expected result:

> encoding
  <{A}> <{B}> <{D}> <{F}> <{A,F}> <{B,F}> <{D},{F}> <{D},{B,F}> <{A,B,F}>
1  TRUE  TRUE  TRUE  TRUE    TRUE    TRUE      TRUE        TRUE      TRUE
2  TRUE  TRUE FALSE  TRUE    TRUE    TRUE     FALSE       FALSE      TRUE
3  TRUE  TRUE FALSE  TRUE    TRUE    TRUE     FALSE       FALSE      TRUE
4  TRUE  TRUE  TRUE  TRUE   FALSE    TRUE      TRUE        TRUE     FALSE
  <{A,B}> <{D},{B}> <{B},{A}> <{D},{A}> <{F},{A}> <{D},{F},{A}> <{B,F},{A}>
1    TRUE      TRUE      TRUE      TRUE      TRUE          TRUE        TRUE
2    TRUE     FALSE     FALSE     FALSE     FALSE         FALSE       FALSE
3    TRUE     FALSE     FALSE     FALSE     FALSE         FALSE       FALSE
4   FALSE      TRUE      TRUE      TRUE      TRUE          TRUE        TRUE
  <{D},{B,F},{A}> <{D},{B},{A}>
1            TRUE          TRUE
2           FALSE         FALSE
3           FALSE         FALSE
4            TRUE          TRUE
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!