我有data.frame
格式的数据,我希望将其转换为transactions
或itemMatrix
。
检查arules
中的功能是否支持这两种数据格式,这就是我问这个问题的原因
答案 0 :(得分:5)
library(arules)
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
trans2 <- as(a_matrix, "transactions")
trans2
inspect(trans2)
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
trans3 <- as(a_df, "transactions")
image(trans3)