0
類別
我有3列的數據幀(DF)兩個字符串:識別號,類別和品牌:R:找到最常見一起每
ID Category Brand
00129 Bits B89
00129 Bits B87
00129 Bits B87
00129 Logs B32
00129 Logs B27
00129 Logs B27
00130 Bits B12
00130 Bits B14
00130 Bits B14
00131 Logs B32
00131 Logs B27
00131 Logs B32
00132 Bits B77
00132 Bits B89
00132 Bits B89
我有200個不同的類別和2000不同品牌。
我想通過ID號找到每最經常一起購買的類別中的兩個品牌:
Category Brand
Bits B89,B87
Logs B32,B27
或:
#$Bits
#[1] "B89" "B87"
#$Logs
#[1] "B32" "B27"
我能想到的唯一辦法是返工的數據幀看起來像這樣以確保它是通過確認不同的ID號來計算的:
B89 B87 B32 B27 B12 B14
1 1 2 1 2 0 0
2 0 0 0 0 1 2
3 0 0 2 1 0 0
4 2 1 0 0 0 0
然後返回已填充了值大於0的列當某一列都將填充大於0
list1 =(setNames(object = lapply(1:NCOL(df), function(i)
unique(colnames(df)[-i][which(as.matrix(df[which(df[,i] > 0),i])>0,
arr.ind = TRUE)[,2]])),
nm = colnames(df)))
值但後來我犧牲的範疇,我需要的。有關如何解決這個問題的任何想法?