Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Generate counts within group and subgroup

$
0
0

I want to create a column that counts how many times in a row an incident occurs. Imagine flipping a coin 100 times and you count the number of heads ("H") and tales ("T") that occur in a row:

outcome: "H","H","H","H","T","T","T","H","T","T",...

Count: 1,2,3,4,1,2,3,1,1,2...

I can achieve this by the following syntax: df$count <- sequence(rle(df$outcome)$lengths) - 0

However, Imagine that i take rounds within the 100 coin tosses. The rounds are of unequal lengths, and I need to separate between them in the count column:

Toss   Round  Outcome  Count
1      1      H        1
2      1      H        2
3      1      H        3
4      1      H        4
5      1      T        1
6      2      T        1
7      2      T        2
8      2      H        1
...

How can I implement such a distinction (i.e. group by rounds while counting within outcome) between the rounds in my code?

Edit: Just wanted to ad that this is a very simplified version of something i need to do in a 46.000 row dataset, so the solution cannot be based on the table I provide, but preferrable into the rle-code or equal.

Thanks!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>