2014-02-28 96 views
1

假設我有一個帶有因子的data.frame。計算data.frame中某個元素出現的次數

d = data.frame(f = c("a","a","a","b","b","b","b","d","d")) 

    f 
1 a 
2 a 
3 a 
4 b 
5 b 
6 b 
7 b 
8 d 
9 d 

而我想添加一個欄,告訴我一個元素出現的次數。

像這樣

f n 
1 a 3 
2 a 3 
3 a 3 
4 b 4 
5 b 4 
6 b 4 
7 b 4 
8 d 2 
9 d 2 

我將如何做到這一點?

回答

1

您可以使用table這樣的:

d$n <- table(d$f)[d$f] 
# f n 
#1 a 3 
#2 a 3 
#3 a 3 
#4 b 4 
#5 b 4 
#6 b 4 
#7 b 4 
#8 d 2 
#9 d 2 
1

您可以使用avelength

> d$n <- as.numeric(ave(as.character(d$f), d$f, FUN = length)) 
> d 
    f n 
1 a 3 
2 a 3 
3 a 3 
4 b 4 
5 b 4 
6 b 4 
7 b 4 
8 d 2 
9 d 2 

隨着 「data.table」 包,你可以這樣做:

library(data.table) 
D <- data.table(d) 
D[, n := as.numeric(.N), by = f] 
2

還可以用一些plyr好玩ctions - join & ddply

d <- data.frame(f = c("a","a","a","b","b","b","b","d","d")) 
d2 <- join(d, ddply(d, .(f), 'nrow')) 
d2 
    f nrow 
1 a 3 
2 a 3 
3 a 3 
4 b 4 
5 b 4 
6 b 4 
7 b 4 
8 d 2 
9 d 2