I have used a table (23 rows) where I extracted 2 columns (called them x and y) and named z=x-y and w=x-y. I also have a column called "type" which has 2 variables, "buy" or "sell".
I have an "if" statement which states: If (type=="buy", z, w)
So, if i=1, type=buy, i want R to return z[1]. However, when I wrote down the loop, it returns 23 results for each "i". I have tried using the [[i]] brackets but still no result.
for(i in 1:23)
{
z[i]=y[i]-x[i]
w[i]=x[i]-y[i]
ifelse(type[i]=="buy", z[i], w[i])
}
Code without the for loop:
z=y-x #buy
w=x-y #sell
ifelse(type=="buy",z, w)
Any advice? Thank you
Aucun commentaire:
Enregistrer un commentaire