vendredi 23 février 2018

Increase the value of a variable in a vector using ifelse if R

I have an ifelse statement and want to increase the the variable by 1 each time the condition is met and drop the count to by subtracting the entire count -count if the condition is not met.

df = data.frame(indicator= c("60","61","58","40","70","80"))

position = 0
ifelse((df$indicator > "60"), 
       (position = position + 1), 
       -position)

This is to keep track of a trading position (buy if over 60 and sell if under but keep track of how big the position is.

How can I do this in R?

Aucun commentaire:

Enregistrer un commentaire