lundi 23 juillet 2018

Compute cumsum if a condition is found

Suppose I have a jackpot machine and I want to keep track of the money in the pot. The pot starts from 1mil dollars and it grows by $1 every time someone place a bet.

I would like to achieve this.

    bet    outcome   meter    cost
    1      NA        1000000  0    
    1      NA        1000001  0
    1      NA        1000002  0
    1      Minor     1000003  500
    1      NA        999503   0
    1      NA        999504   0
    1      Major     999505   999505

The code I tried but returned NA due to overflow.

df$cost <- ifelse(df$outcome == "Major", cumsum(df$bet - df$cost),
                 ifelse(df$outcome == "Minor", 500, 0))

I would like to know if it is possible to extract a single value from the cumsum function.

cumsum(...)[Line where Major is hit]

Aucun commentaire:

Enregistrer un commentaire