我正在尝试使用带if else语句的foreach循环rbind多个数据帧。在添加if else语句之前,数据帧能够重新绑定并平稳运行。但是在添加了if else语句之后,结果表(M_test)变为NULL,但是foreach循环完全运行而没有任何错误。
system.time(
M_test <- foreach(h = 1:50, .combine=rbind, .packages="data.table") %dopar% {
# Convert datafram to vector by row
v_items <- temp2[h,]
# Remove blank & NA
v_items <- v_items[v_items != "NA"]
v_items <- v_items[v_items != ""]
rules <- arules::apriori(trans,parameter=list(supp=0.001,conf=0.1, target = "rules", minlen=2),
appearance=list(lhs=c(as.character(v_items)),default="rhs"))
if(length(rules)!=0){
rules_df <- arules::DATAFRAME(rules)
data.table(
Key=T_test[h,1],
LHS=rules_df$LHS,
RHS=rules_df$RHS,
support=rules_df$support,
confidence=rules_df$confidence,
lift=rules_df$lift,
count=rules_df$count
)}
else{
rules_df <- data.frame(LHS=as.character("NA"),
RHS=as.character("NA"),
support=as.numeric(0),
confidence=as.numeric(0),
lift=as.numeric(0),
count=as.numeric(0))
data.table(
Key=T_test[h,1],
LHS=rules_df$LHS,
RHS=rules_df$RHS,
support=rules_df$support,
confidence=rules_df$confidence,
lift=rules_df$lift,
count=rules_df$count
)
}
}
)