df_loop <- data.frame(data1, data2, data3, 0)
for (i in 4:nrow(df_loop[1]))
{
tmp_1 = df_loop[i,1]
tmp_2 = df_loop[i,2]
tmp_2_lag3 = df_loop[i-3,2]
tmp_3 = df_loop[i,3]
if (!is.na(tmp_2_lag3))
{
if((tmp_1 > tmp_2) & (tmp_2 > tmp_2_lag3) & (tmp_3 > 1.5))
{
df_loop[i,4] = 2
}
else
{
if((tmp_1 < tmp_2) & (tmp_2 < tmp_2_lag3) & (tmp_3 < -1.5))
{
df_loop[i,4] = 4
}
else
{
if (df_loop[i-1,4] == 2)
{
df_loop[i,4] = 3
}
else
{
if (df_loop[i-1,4] == 4)
{
df_loop[i,4] = 1
}
else
{
df_loop[i,4] = df_loop[i-1,4]
}
}
}
}
}
}