BI Dude
BI Dude

Reputation: 2016

R - How to Count Rows in A Data Set by Category

I am working on simple question but cannot find out how to achieve a simple aggregation in R. I want to count rows per category (ID) in a data set with an example below:

Date    Col1    Col2    ID
21/01/2003  1   2   1
27/01/2003  3   6   2
28/01/2003  4   5   2
29/01/2003  5   6   3
30/01/2003  1   0   2

I have tried *apply functions without luck. The closest answer I could get to my question is using aggregate function:

aggregate(fact, by=list(fact$ID), FUN=length)

The problem is that it displays all columns in a data set rather than just ID & count. How can i make it better.

    Group.1 Date    Col1    Col2  ID
1         1  117     117     117  117
2         2 1041    1041    1041 1041
3         3  243     243     243  243
4         4  474     474     474  474

Ideally: I want to rename ID to Category and Count into Volume so it looks like this:

    Category Volume 
         1  117 
         2 1041 
         3  243 
         4  474 

Update:

  1. I have installed "plyr" & "data.table" packages. And both solutions work perfectly fine.
  2. How to achieve the same solution using out of the box R?

Upvotes: 0

Views: 2456

Answers (2)

josliber
josliber

Reputation: 44320

Usually when I want to count the frequency of different values for one variable I use the table function instead of aggregate. You can pass the output of table to the data.frame function to get the data structure you want (I used setNames to set the variable names):

(counts <- setNames(data.frame(table(dat$ID)), c("Category", "Volume")))
#   Category Volume
# 1        1      1
# 2        2      3
# 3        3      1

Upvotes: 4

Rich Scriven
Rich Scriven

Reputation: 99331

You can use count from plyr

> library(plyr)
> setNames(count(df, "ID"), c("Category", "Volume"))
#   Category Volume
# 1        1      1
# 2        2      3
# 3        3      1

Upvotes: 2

Related Questions