2013-07-09 86 views
1

我有一個這樣的數據集。特定類別的頻率計數

a <- structure(list(Prone = c("M", "N", "N", "N", "M", "N", "M", "N", "M", "M"), 
Type = c("A", "B", "C", "A", "A", "A", "B", "B", "C", "B"), 
Alc = c("A", "B", "N", "A", "A", "A", "B", "B", "B", "B"), 
Com = c("Y", "N", "Y", "Y", "Y", "Y", "Y", "N", "N", "Y")), 
.Names = c("Prone", "Type", "Alc", "Com"), row.names = c(NA, -10L), class = "data.frame") 
a 
    Prone Type Alc Com 
1  M A A Y 
2  N B B N 
3  N C N Y 
4  N A A Y 
5  M A A Y 
6  N A A Y 
7  M B B Y 
8  N B B N 
9  M C B N 
10  M B B Y 

我想了解每個唯一行的頻率計數如下所示:提前

Prone Type Alc Com Freq 
1  M A A Y 2 
2  M B B Y 2 
3  M C B N 1 
4  N A A Y 2 
5  N B B N 2 
6  N C N Y 1 

感謝。

+1

而搞什麼名堂,你試圖到目前爲止和你在哪裏卡住了? – Arun

+0

我想使用plyr包和內置的表函數。但我沒有按要求獲得頻率。 –

回答

6

有很多方法可以做到這一點,這裏是一個簡單plyr例如:

> library(plyr) 
> ddply(a,names(a),summarize,Freq=length(Prone)) 
    Prone Type Alc Com Freq 
1  M A A Y 2 
2  M B B Y 2 
3  M C B N 1 
4  N A A Y 2 
5  N B B N 2 
6  N C N Y 1 
6

使用基地aggregate

aggregate(data = transform(a, Freq = seq_len(nrow(a))), Freq ~ ., length) 

    Prone Type Alc Com Freq 
1  N B B N 2 
2  M C B N 1 
3  M A A Y 2 
4  N A A Y 2 
5  M B B Y 2 
6  N C N Y 1 
3

這裏的另一種方法:

library(qdap) 
colsplit2df(data.frame(table(paste2(a))), new.names = names(a)) 

## > colsplit2df(data.frame(table(paste2(a))), new.names = names(a)) 
## Prone Type Alc Com Freq 
## 1  M A A Y 2 
## 2  M B B Y 2 
## 3  M C B N 1 
## 4  N A A Y 2 
## 5  N B B N 2 
## 6  N C N Y 1 
+1

'qdap'當然似乎加載了很多這樣漂亮的包裝。我要檢查它... – Arun

6

強制性data.table解決方案:

library(data.table) 
dt = data.table(a) 

dt[, list(Freq = .N), by = names(dt)] 
+0

總是喜歡看到data.table解決方案+1 –

8

備用plyr解決方案:

> library("plyr") 
> count(a) 
    Prone Type Alc Com freq 
1  M A A Y 2 
2  M B B Y 2 
3  M C B N 1 
4  N A A Y 2 
5  N B B N 2 
6  N C N Y 1 
+0

不知道這個,感謝它。 – David

+0

好方法+1 –