简体   繁体   中英

Frequency count for a specific category

I have a data set like this.

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

I like to get the frequency count of each unique row like the following:

  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

Thanks in advance.

Alternate plyr solution:

> 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

The mandatory data.table solution:

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

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

There are a lot of ways to do this, here is a simple plyr example:

> 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

Using base 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

Here's another approach:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM