I have some code that runs a sports league table standings simulation, using a “for” loop in R. For each iteration of the loop, a table of standings (i.e. a dataframe) df
is produced like this:
Team Points
1 Arsenal 12
2 Chelsea 11
3 Liverpool 9
4 Newcastle 9
5 Man U 7
6 Fulham 6
Suppose I run n
simulations (i.e. something like for(i in 1:n) {...}
I would like to count the number of times a certain team finishes in top 3 position. For instance I want to count Chelsea. How can I add this feature? Obviously I need to add a counter that starts at 0 and increments by 1 every time Chelsea is in top 3 of the table for that respective loop iteration. But how?