2015-10-31 19 views
1

我有以下數據集:拆柱和獨特的價值則合計數

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 

回答

2

嘗試

library(tidyr) 
library(dplyr) 

df %>% 
    gather(arg, value) %>% 
    count(arg, value) %>% 
    summarise(unique_count_values = toString(paste0(value, "(", n, ")"))) 

其中給出:

#Source: local data frame [2 x 2] 
# 
#  arg   unique_count_values 
# (fctr)      (chr) 
#1 color   black(3), green(2) 
#2 type chair(2), sofa(2), table(1) 
+0

這可以使用FNN,plyr或基礎包來實現嗎? – AngryPanda

+0

我想知道任何替代方法,因爲這對我來說似乎有點複雜。 – AngryPanda

+0

此外,我仍然想知道如何轉換我的數據集以獲取arg和值列。 – AngryPanda

1

這裏有一個基礎R的方法。我已經擴大了一些,以便我可以添加有關正在發生的事情的評論。

基本思想是僅使用sapply循環遍歷列,列出每列中的數據,然後使用sprintf提取製表的相關部分以實現所需的輸出(名稱,後跟值括號內的)。

stack函數獲取最終命名向量並將其轉換爲data.frame

stack(      ## convert the final output to a data.frame 
    sapply(     ## cycle through each column 
    mydf, function(x) { 
     temp <- table(x)  ## calculate counts and paste together values 
     paste(sprintf("%s (%d)", names(temp), temp), collapse = " ") 
    })) 
#       values ind 
# 1   black (3) green (2) color 
# 2 chair (2) sofa (2) table (1) type 

如果數據factor S,你也可以嘗試像下面,你所期望的數據相匹配,而不是所需的輸出。

stack(apply(summary(mydf), 2, function(x) paste(na.omit(x), collapse = " "))) 
#       values  ind 
# 1   black:3 green:2  color 
# 2 chair:2 sofa :2 table:1  type