将行数据转换为二进制列

时间:2012-12-05 16:33:36

标签: r

我试图将一列数据格式化为许多二进制列,最终用于关联规则挖掘。我使用for循环和一个简单的三元组矩阵取得了一些成功,但我不确定如何按照后面第一列中的级别进行聚合 - 类似于SQL中的 group by 语句。我在下面提供了一个示例,尽管数据集要小得多 - 如果成功,我的实际数据集将是4,200行乘3,902列,因此任何解决方案都需要可扩展。任何建议或替代方法将不胜感激!

> data <- data.frame(a=c('sally','george','andy','sue','sue','sally','george'), b=c('green','yellow','green','yellow','purple','brown','purple'))
> data
       a      b
1  sally  green
2 george yellow
3   andy  green
4    sue yellow
5    sue purple
6  sally  brown
7 george purple

x <- data[,1]
for(i in as.numeric(2:ncol(data))) 
 x <- cbind(x, simple_triplet_matrix(i=1:nrow(data), j=as.numeric(data[,i]),
              v = rep(1,nrow(data)), dimnames = list(NULL, levels(data[,i]))) )

##Looks like this:

> as.matrix(x)

     name    brown green purple yellow
[1,] "sally"  "0"    "1"   "0"     "0"    
[2,] "george" "0"    "0"   "0"     "1"   
[3,] "andy"   "0"    "1"   "0"     "0"    
[4,] "sue"    "0"    "0"   "0"     "1"   
[5,] "sue"    "0"    "0"   "1"     "0"    
[6,] "sally"  "1"    "0"   "0"     "0" ##Need to aggregate by Name

##Would like it to look like this:
     name    brown green purple yellow
[1,] "sally"  "1"   "1"   "0"    "0"    
[2,] "george" "0"   "0"   "0"    "1"   
[3,] "andy"   "0"   "1"   "0"    "0"    
[4,] "sue"    "0"   "0"   "1"    "1"   

1 个答案:

答案 0 :(得分:4)

这应该可以解决问题:

## Get a contingency table of counts
X <- with(data, table(a,b))

## Massage it into the format you're wanting 
cbind(name = rownames(X), apply(X, 2, as.character))
#      name     brown green purple yellow
# [1,] "andy"   "0"   "1"   "0"    "0"   
# [2,] "george" "0"   "0"   "1"    "1"   
# [3,] "sally"  "1"   "1"   "0"    "0"   
# [4,] "sue"    "0"   "0"   "1"    "1"