代码:
data=read.csv("./spmf1234.csv",header=F);
df<- data.frame(do.call('rbind', strsplit(as.character(data$V1),'#',fixed=F)));
df2<- data.frame(do.call('rbind', strsplit(as.character(df$X1),'==>',fixed=F)));
df3=cbind(df2,df);
colnames(df3)=c("lhs","rhs","rule","support","confidence","lift");
df4 <- subset(df3, select = c(lhs,rhs,support,confidence,lift));
final=subset(df4,lhs!=1);
我正在使用fp-growth从spmf获取的csv文件上尝试上面的代码来获取关联规则。我希望所有规则都包含&#39; 1&#39;在lhs被删除,但这不起作用。
csv文件:
2 ==&gt; 1 #SUP:1 #CONF:0.33333 #LIFT:0.66667
1 ==&gt; 2 #SUP:1 #CONF:0.33333 #LIFT:0.66667
3 ==&gt; 1 #SUP:2 #CONF:0.5 #LIFT:1
1 ==&gt; 3 #SUP:2 #CONF:0.66667 #LIFT:1
3 ==&gt; 2 #SUP:2 #CONF:0.5 #LIFT:1
2 ==&gt; 3 #SUP:2 #CONF:0.66667 #LIFT:1
2 3 ==&gt; 1 #SUP:1 #CONF:0.5 #LIFT:1
1 3 ==&gt; 2 #SUP:1 #CONF:0.5 #LIFT:1
1 2 ==&gt; 3 #SUP:1 #CONF:1 #LIFT:1.5
3 ==&gt; 1 2 #SUP:1 #CONF:0.25 #LIFT:1.5
2 ==&gt; 1 3 #SUP:1 #CONF:0.33333 #LIFT:1
1 ==&gt; 2 3 #SUP:1 #CONF:0.33333 #LIFT:1
答案 0 :(得分:1)
final=subset(df4,lhs!=1)
将字符与数字进行比较,但不起作用:
# > as.character(df4$lhs)
# [1] "2 " "1 " "3 " "1 " "3 " "2 " "2 3 " "1 3 " "1 2 " "3 " "2 " "1 "
你可能想使用像这样的一个正则表达式:
final = subset(df4, !grepl("1\\b", lhs))
# > final
# lhs rhs support confidence lift
# 1 2 1 SUP: 1 CONF: 0.33333 LIFT: 0.66667
# 3 3 1 SUP: 2 CONF: 0.5 LIFT: 1
# 5 3 2 SUP: 2 CONF: 0.5 LIFT: 1
# 6 2 3 SUP: 2 CONF: 0.66667 LIFT: 1
# 7 2 3 1 SUP: 1 CONF: 0.5 LIFT: 1
# 10 3 1 2 SUP: 1 CONF: 0.25 LIFT: 1.5
# 11 2 1 3 SUP: 1 CONF: 0.33333 LIFT: 1
添加:
## data preperation
data <- readLines(con = textConnection("
2 ==> 1 #SUP: 1 #CONF: 0.33333 #LIFT: 0.66667
1 ==> 2 #SUP: 1 #CONF: 0.33333 #LIFT: 0.66667
3 ==> 1 #SUP: 2 #CONF: 0.5 #LIFT: 1
1 ==> 3 #SUP: 2 #CONF: 0.66667 #LIFT: 1
3 ==> 2 #SUP: 2 #CONF: 0.5 #LIFT: 1
2 ==> 3 #SUP: 2 #CONF: 0.66667 #LIFT: 1
2 3 ==> 1 #SUP: 1 #CONF: 0.5 #LIFT: 1
1 3 ==> 2 #SUP: 1 #CONF: 0.5 #LIFT: 1
1 2 ==> 3 #SUP: 1 #CONF: 1 #LIFT: 1.5
3 ==> 1 2 #SUP: 1 #CONF: 0.25 #LIFT: 1.5
2 ==> 1 3 #SUP: 1 #CONF: 0.33333 #LIFT: 1
1 ==> 2 3 #SUP: 1 #CONF: 0.33333 #LIFT: 1"))
r <- regexec(pattern = "([0-9 ]+)\\s==>\\s([0-9 ]+)\\s\\#SUP:\\s([0-9.]+)\\s\\#CONF:\\s([0-9.]+)\\s\\#LIFT:\\s([0-9.]+)",
text = data)
m <- regmatches(data, r)
df <- setNames(as.data.frame(do.call(rbind, lapply(m, "[", -1)), stringsAsFactors = FALSE),
c("lhs", "rhs", "support", "confidence", "lift"))
## rows to include/exclude
include <- sapply(strsplit(df$lhs, " "), function(x) !any(as.integer(x) %in% 1:1000))
df[include, ]