Maybe this will help you, using dplyr
Data test frame
dados <- data.frame(coluna_1 = c(558.8, 584.3, 603.3))
The logic
library(dplyr)
dados <- dados %>% mutate(coluna_2 = case_when(
coluna_1 - lag(coluna_1, n = 2) >= lag(coluna_1, n = 2) * 0.02 ~ 1,
coluna_1 - lag(coluna_1, n = 2) <= (-lag(coluna_1, n = 2)) * 0.02 ~ -1,
T ~ 0
))
dados$coluna_2[1:2] <- 1
With mutate we create a new column called column_2, the case_when serves to do something when a condition is accepted, the lag is to give a delay, in this case we give a lag of 2 which would be two lines above. T ~ 0 is when no condition was found then fill with 0.
Note: As the calculation is done by returning two lines the first results will be zero, because there is no way to calculate.
A Python possibility using pandas
import pandas as pd
dados = pd.DataFrame({'coluna_1': [558.8, 584.3, 603.3]})
dados.loc[dados['coluna_1'] - dados['coluna_1'].shift(2) >= dados['coluna_1'].shift(2) * 0.02, 'coluna_2'] = 1
dados.loc[dados['coluna_1'] - dados['coluna_1'].shift(2) <= -dados['coluna_1'].shift(2) * 0.02, 'coluna_2'] = -1
dados['coluna_2'].iloc[0:2] = 1
dados.fillna(0)
Thank you so much for the lesson, it helped me a lot, would have some way to leave the first items with the value 1?
– Roney Wesley Galan
@Roneywesleygalan, good afternoon! As the first two values do not enter into the conditions of fulfillment of the remainder, we can place 1 arbitrarily. I updated the code to resolve this issue. If conditions are always met, you can put fill_na or T ~ equal to 1 as they will be the only missing results. Example:
fill_na(1)
or in RT ~ 1
Hug!– lmonferrari
Thank you so much!! Helped me so much, one day I arrive at this level expert rsrs.
– Roney Wesley Galan
@Roneywesleygalan, for nothing! needing we are there! It will surely come! Hug!!!
– lmonferrari