-2

For a sample dataframe:

df1 <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 5L), c.1 = c(21L, 22L, 33L, 43L, 12L, 33L, 21L, 
54L, 32L, 45L, 24L, 11L, 5L, 2L, 1L), count = c(15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L)), .Names = c("id", 
"c.1", "count"), row.names = c(NA, -15L), class = "data.frame")

I wish to count the number of data points in col.1 BY the id column. For example in the above dataframe, I want a column, 'counts' to be added which records how many rows of data there are for each of the ids i.e. the count column would record 3s for ids 1, 2 and 3 and then 4s for id 4 and 1 for id 5.

I have been trying to use length but I cant seem to include a 'by' type command to look at id.

Any help would be appreciated.

KT_1
  • 8,194
  • 15
  • 56
  • 68

3 Answers3

0

A base R solution:

within(df1, count <- ave(c.1, id, FUN = length))
#   id c.1 count
#1   1  21     3
#2   1  22     3
#3   1  33     3
#4   2  43     3
#5   2  12     3
#6   2  33     3
#7   3  21     3
#8   3  54     3
#9   3  32     3
#10  4  45     5
#11  4  24     5
#12  4  11     5
#13  4   5     5
#14  4   2     5
#15  5   1     1

Note that there are about a dozen more possibilities (including functionality of some packages) to do this. Some more efficient by orders of magnitude for big data.

Roland
  • 127,288
  • 10
  • 191
  • 288
0

Here is my dplyr solution

> df1 %>% group_by(id) %>% mutate(count = n())
Source: local data frame [15 x 3]
Groups: id

   id c.1 count
1   1  21     3
2   1  22     3
3   1  33     3
4   2  43     3
5   2  12     3
6   2  33     3
7   3  21     3
8   3  54     3
9   3  32     3
10  4  45     5
11  4  24     5
12  4  11     5
13  4   5     5
14  4   2     5
15  5   1     1
SabDeM
  • 7,050
  • 2
  • 25
  • 38
0

I would recommend to use plyr for this. Here some example how to do it:

require(plyr)
df2<-ddply(df1,.(id),summarize,number=length(count))

This is my output:

df2
  id number
1  1      3
2  2      3
3  3      3
4  4      5
5  5      1
Sarina
  • 548
  • 3
  • 10