Reputation: 107
I am using R to analyze data and I need to count how many consecutive values in a data frame belongs to a certain group. The group is given by the column "type". Here is an example of my data. The date column won't be used, it was useful just to order the rows.
date type
2009-11-12 02:01:02 1
2009-11-12 02:08:27 1
2009-11-12 02:26:44 1
2009-11-12 02:27:12 1
2009-11-12 02:28:14 1
2009-11-12 02:30:04 1
2009-11-12 02:35:19 5
2009-11-12 02:40:11 2
2009-11-12 11:35:04 6
2009-11-12 12:32:06 4
2009-11-12 12:32:24 6
2009-11-12 14:08:00 5
2009-11-12 14:16:00 1
2009-11-12 14:17:33 1
I know I can use ddply for example to summarize data. However I want summaries breaking up by changes in the column "type". Applying ddply will give me something like this:
> df <- data.frame(x=c(1,1,1,1,1,1,5,2,6,4,6,5,1,1))
> ddply(df,c("x"),summarize,count=length(x))
x count
========
1 8
2 1
4 1
5 2
6 2
Whereas, what I really want to achieve is this:
x count
========
1 6
5 1
2 1
6 1
4 1
6 1
5 1
1 2
Upvotes: 3
Views: 1548
Reputation: 173577
The function you're looking for is rle
:
df <- data.frame(x=c(1,1,1,1,1,1,5,2,6,4,6,5,1,1))
> rle(df$x)
Run Length Encoding
lengths: int [1:8] 6 1 1 1 1 1 1 2
values : num [1:8] 1 5 2 6 4 6 5 1
Upvotes: 6