将 r 中的数据帧转换为事务或 itemMatrix?

2024-04-24

我有一个数据data.frame我想将其转换成的格式transactions or an itemMatrix.

检查功能arules支持这两种数据格式这就是我问这个问题的原因


library(arules)

示例 1:从矩阵创建交易

a_matrix <- matrix(
      c(1,1,1,0,0,
    1,1,0,0,0,
    1,1,0,1,0,
    0,0,1,0,1,
    1,1,0,1,1), ncol = 5)

设置暗淡名称

dimnames(a_matrix) <-  list(
    c("a","b","c","d","e"),
    paste("Tr",c(1:5), sep = ""))

a_matrix

coerce

trans2 <-  as(a_matrix, "transactions")
trans2
inspect(trans2)

示例 2:从 data.frame 创建交易

a_df <- data.frame(
    age = as.factor(c(6,8,7,6,9,5)), 
    grade = as.factor(c(1,3,1,1,4,1)))  

注意:所有属性都必须是因子

a_df

coerce

trans3 <- as(a_df, "transactions") 
image(trans3)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 r 中的数据帧转换为事务或 itemMatrix? 的相关文章

随机推荐