dimanche 23 juillet 2017

Conditioning data frames

I am building a data frame to decide when to buy and sell stocks.

My first data frame looks something like this, where 1 indicates a "buy" and -1 that a "sell"

AAPL <- c(1,1,1,0,0,-1,0,-1,-1,0,0,0,1)
df <- data.frame (AAPL)

What I would like is to have something like this were I never buy a stock that I have not yet sold.

AAPL.new <- c(1,0,0,0,0,-1,0,0,0,0,0,0,1)
df.new <- data.frame (AAPL.new)

So far, I have been using ifelse() but I can only avoid the repetition in consecutive observations, I cannot impose the condition that after a "-1" I have to have a "0" or a "1".

I am a begginer moving from small samples in Excel to larger samples in R.

Thank you in advance for your help.

Aucun commentaire:

Enregistrer un commentaire