我有以下數據集:拆柱和獨特的價值則合計數
color type
1 black chair
2 black chair
3 black sofa
4 green table
5 green sofa
我想拆分此形成以下數據集:
arg value
1 color black
2 color black
3 color black
4 color green
5 color green
6 type chair
7 type chair
8 type sofa
9 type table
10 type sofa
然後,我會想計算獨特所有arg值組合的值:
arg value count
1 color black 3
2 color green 2
3 type chair 2
4 type sofa 2
5 type table 1
它不需要按count進行排序。這將被印在下面的輸出形式:
arg unique_count_values
1 color black(3) green(2)
2 type chair(2) sofa(2) table(1)
我試過如下:
AttrList<-colnames(DataSet)
aggregate(.~ AttrList, DataSet, FUN=function(x) length(unique(x)))
我也試過summary(DataSet)
但當時我不知道如何操作,結果得到它在所需的輸出形式。
我對R比較陌生。如果你發現一些會減少工作的東西,請讓我知道。謝謝!
更新
所以,我試過如下:
x <- matrix(c(101:104,101:104,105:106,1,2,3,3,4,5,4,5,7,5), nrow=10, ncol=2)
V1 V2
1 101 1
2 102 2
3 103 3
4 104 3
5 101 4
6 102 5
7 103 4
8 104 5
9 105 7
10 106 5
轉換爲table
:
as.data.frame(table(x))
這給了我:
x Freq
1 1 1
2 2 1
3 3 2
4 4 2
5 5 3
6 7 1
7 101 2
8 102 2
9 103 2
10 104 2
11 105 1
12 106 1
我應該怎麼做,所以我得到這樣的:
V Val Freq
1 V2 1 1
2 V2 2 1
3 V2 3 2
4 V2 4 2
5 V2 5 3
6 V2 7 1
7 V1 101 2
8 V1 102 2
9 V1 103 2
10 V1 104 2
11 V1 105 1
12 V1 106 1
這可以使用FNN,plyr或基礎包來實現嗎? – AngryPanda
我想知道任何替代方法,因爲這對我來說似乎有點複雜。 – AngryPanda
此外,我仍然想知道如何轉換我的數據集以獲取arg和值列。 – AngryPanda