Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

r - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...