Capture the output of arules::inspect as data.frame

走远了吗. 提交于 2021-01-29 10:38:36

问题


In "Zero frequent items" when using the eclat to mine frequent itemsets, the OP is interested in the groupings/clusterings based on how frequent they are ordered together. This grouping can be inspected by the arules::inspect function.

library(arules)
dataset <- read.transactions("8GbjnHK2.txt", sep = ";", rm.duplicates = TRUE)
f <- eclat(dataset, 
           parameter = list(
             supp = 0.001, 
             maxlen = 17, 
             tidLists = TRUE))
inspect(head(sort(f, by = "support"), 10))

The data set can be downloaded from https://pastebin.com/8GbjnHK2.

However, the output cannot be easily saved to another object as a data frame.

out <- inspect(f)

So how do we capture the output of inspect(f) for use as data frame?


回答1:


We can use the methods labels to extract the associations/groupings and quality to extract the quality measures (support and count). We can then use cbind to store these into a data frame.

out <- cbind(labels = labels(f), quality(f))
head(out)

#              labels  support count
# 1 {3031093,3059242} 0.001010    16
# 2 {3031096,3059242} 0.001073    17
# 3 {3060614,3060615} 0.001010    16
# 4 {3022540,3072091} 0.001010    16
# 5 {3061698,3061700} 0.001073    17
# 6 {3031087,3059242} 0.002778    44



回答2:


Coercing the itemsets to a data.frame also creates the required output.

> head(as(f, "data.frame"))
              items     support count
1 {3031093,3059242} 0.001010101    16
2 {3031096,3059242} 0.001073232    17
3 {3060614,3060615} 0.001010101    16
4 {3022540,3072091} 0.001010101    16
5 {3061698,3061700} 0.001073232    17
6 {3031087,3059242} 0.002777778    44


来源:https://stackoverflow.com/questions/50554355/capture-the-output-of-arulesinspect-as-data-frame

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