我正在PowerBI中使用可视化的r脚本。我可以在R中运行以下代码,并且可以运行,但是在PowerBI中运行时出现以下错误。我想在视觉上显示先验的结果。下面的两个测试都可以在R中正常运行,但不能在PowerBI的R脚本视觉中运行。有什么想法吗?
如果我尝试#test2。
library(Matrix)
library(arules)
library(plyr)
library(gridExtra)
df_itemList <- ddply(dataset,c("SALESID"),function(df1)paste(df1$ITEMID))
#test1
#df_itemList = sapply(df_itemList , function(x) gsub(" ", ",", x))
#basket_rules <- apriori(df_itemList, parameter = list(sup=0.1,conf=0.5,target="rules", maxlen=5));
#test2
txn = read.transactions(df_itemList, rm.duplicates = TRUE, format = "basket", sep = ",", cols = 1);
basket_rules <- apriori(txn, parameter = list(sup=0.1,conf=0.5,target="rules", maxlen=5));
df_basket <- as(basket_rules,"data.frame")
grid.table(df_basket)
错误消息: R脚本错误。
附加程序包:“ arules”
以下对象被“ package:base”屏蔽:
abbreviate, write
readLines(file,encoding = encoding)中的错误:'con'不是连接 调用:read.transactions-> lapply-> readLines 执行停止
如果我尝试#test1 ...
library(Matrix)
library(arules)
library(plyr)
library(gridExtra)
df_itemList <- ddply(dataset,c("SALESID"),function(df1)paste(df1$ITEMID))
#test1
df_itemList = sapply(df_itemList , function(x) gsub(" ", ",", x))
basket_rules <- apriori(df_itemList, parameter = list(sup=0.1,conf=0.5,target="rules", maxlen=5));
#test2
#txn = read.transactions(df_itemList, rm.duplicates = TRUE, format = "basket", sep = ",", cols = 1);
#basket_rules <- apriori(txn, parameter = list(sup=0.1,conf=0.5,target="rules", maxlen=5));
df_basket <- as(basket_rules,"data.frame")
grid.table(df_basket)
然后我得到下面的错误。
错误消息: R脚本错误。
附加程序包:“ arules”
以下对象被“ package:base”屏蔽:
abbreviate, write
asMethod(object)中的错误: 第2、3、4列不是逻辑或因素。首先离散化列。 调用:apriori-> as-> asMethod 执行停止
答案 0 :(得分:0)
在PowerBI R脚本中使用read.transactions的正确方法是将数据帧转换为矩阵,然后转换为事务类。这是要通过导出到csv,然后再读回read.transactions ...参考here
library(Matrix)
library(arules)
library(plyr)
library(dplyr)
library(gridExtra)
itemList <- dataset
#itemList <- read.csv("ItemListAll.csv", header=TRUE, sep=",")
# Converting to a Matrix ####
itemList$const = TRUE
# Remove duplicates
dim(itemList)
orders <- unique(itemList)
dim(itemList)
# Need to reshape the matrix
itemList_max_prep <- reshape(data = itemList,
idvar = "SALESID",
timevar = "ITEMID",
direction = "wide")
# Drop the SALESID
itemList_matrix <- as.matrix(itemList_max_prep[,-1])
# Clean up the missing values to be FALSE
itemList_matrix[is.na(itemList_matrix)] <- FALSE
# Clean up names
colnames(itemList_matrix) <- gsub(x=colnames(itemList_matrix),
pattern="const\\.", replacement="")
itemList_trans <- as(itemList_matrix,"transactions")
#inspect(itemList_trans)
basket_rules <- apriori(itemList_trans, parameter = list(sup=0.01,conf=0.5,target="rules", minlen=3));
df_basket <- as(basket_rules,"data.frame")
df_basket$support <- ceiling(df_basket$support * 100)
df_basket$confidence<- ceiling(df_basket$confidence * 100)
df_basket$lift<- round(df_basket$lift, digits = 2)
df_basket <- df_basket[rev(order(df_basket$support)),];
grid.table(head(df_basket));