Skip to content
Prev 67759 / 398506 Next

aggregation question

Hi I have a question concerning aggregation

(simple demo code S. below)

I have the data.frame

    id        meas date
1   a 0.637513747    1
2   a 0.187710063    2
3   a 0.247098459    2
4   a 0.306447690    3
5   b 0.407573577    2
6   b 0.783255085    2
7   b 0.344265082    3
8   b 0.103893068    3
9   c 0.738649586    1
10  c 0.614154037    2
11  c 0.949924371    3
12  c 0.008187858    4

When I want for each id the sum of its meas I do:

	aggregate(data$meas, list(id = data$id), sum)

If I want to know the number of meas(ures) for each id I do, eg

	aggregate(data$meas, list(id = data$id), length)

NOW: Is there a way to compute the number of meas(ures) for each id with 
not identical date (e.g using diff()?
so that I get eg:

   id x
1  a 3
2  b 2
3  c 4


I am sure it must be possible

thanks for any (even short) hint

cheers
Christoph



--------------
data <- data.frame(c(rep("a", 4), rep("b", 4), rep("c", 4)),
                    runif(12), c(1, 2, 2, 3, 2, 2, 3, 3, 1, 2, 3, 4))
names(data) <- c("id", "meas", "date")

m <- aggregate(data$meas, list(id = data$id), sum)
names(m) <- c("id", "cum.meas")