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

提前致谢。

4

5 回答 5

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
于 2013-07-09T21:47:46.103 回答
7

强制data.table解决方案:

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

dt[, list(Freq = .N), by = names(dt)]
于 2013-07-09T21:28:21.187 回答
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
于 2013-07-09T20:24:51.713 回答
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
于 2013-07-09T20:28:34.090 回答
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
于 2013-07-09T20:30:34.490 回答