Count profiles in R
2
0
Entering edit mode
2.9 years ago

Hi everybody, I would like to count the occurrences in a dataframe, for example if my dataframe is:

0 0 0 
0 1 0 
0 0 0
1 1 1 
0 1 0

I would like to have the count of each unique combinations.

000 : 3
010 : 2
111 : 1

Anybody have ideas on how I can do it on R ? Thank you.

R • 832 views
ADD COMMENT
3
Entering edit mode
2.9 years ago

I found the function count() of the library plyr. It does exactly what I want. Thank you!

ADD COMMENT
1
Entering edit mode

table(df$col1,df$col2) does exactly the same as df %>% count(col1, col2). No reason to use bloated tidyverse when base R can do it :)

EDIT: actually there's a difference: count will not include cases with 0 occurrences. It may or may not be desirable.

ADD REPLY
1
Entering edit mode
2.9 years ago

Use table().

ADD COMMENT

Login before adding your answer.

Traffic: 1892 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6