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 "ifelse" statement which states: If (type=="buy", z, w)
So, if i=1, type=buy, i want R to return z[[1]], i.e. the 1st answer from i=1 (not 23 answers for i=1). However, when I wrote down the loop, it returns 23 results for each "i". (i.e. for i=1, it returns 23 results for i=1). 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