将R中的数据帧转换为事务或itemMatrix? [英] convert data frame in r to transactions or an itemMatrix?

查看:176
本文介绍了将R中的数据帧转换为事务或itemMatrix?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式为 data.frame 的数据,我想将其转换为 transactions itemMatrix

I have a data that in data.frame format I want to convert it into transactions or an itemMatrix.

arules 中的Inspects功能支持这两种数据格式这就是为什么我问这个问题

Inspects function in arules support these two data format that's why I'm asking this question

推荐答案

library(arules)



示例1:根据矩阵创建交易



example 1: creating transactions from a matrix

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)



设置暗名



set dim names

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:创建交易记录om data.frame



example 2: creating transactions from 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)))  



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



note: all attributes have to be factors

a_df



强制



coerce

trans3 <- as(a_df, "transactions") 
image(trans3)

这篇关于将R中的数据帧转换为事务或itemMatrix?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆