hello, i am wondering why this code is not working. So i have a table of close prices and i am trying to create a new column called 'triangle' where if the close price of next line is greater than the close price the previous one, assign a value of 2 else assign a value of 1. I am getting an argument of length zero and i do not understand why
Your current code is likely not working because your n gets out of bounds. When you are on your last value of n, you try to access data[nrow(data)+1, ]$Close but that doesn't exist. You might want your loop to go from 1:(nrow(data)-1)
I would use the lag or lead function to solve as shown here - it's a bit unclear which one you want but one of these two examples should be it: