Convert row data to binary columns

前端 未结 1 1159
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 20:33

I am attempting to format a column of data into many binary columns to eventually use for association rule mining. I have had some success using a for loop and a simple trip

相关标签:
1条回答
  • 2020-12-31 20:56

    This should do the trick:

    ## 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"   
    
    0 讨论(0)
提交回复
热议问题