Reputation: 11524
I am asking a simple question. Why are these two code fragments not the same?
pNl <- {}
for(i in length(x):length(x)-250) {
temp <-x[i] - x[i-1]
pNl <-append(pNl, temp, length(pNl))
}
pNl
and
PnL <- {}
for(i in length(x):(length(x)-250)) {
temp <- x[i] - x[i-1]
PnL <- append(PnL, temp, length(PnL))
}
PnL
I get different results when I execute them...
I really appreciate your answer!!!
Upvotes: 0
Views: 315
Reputation: 60452
The problem is in the for
loop definition. In the first loop statement you have left off brackets, i.e.
#length(x):length(x)-250
R> 2:5-1
[1] 1 2 3 4
In the second loop you have brackets, so:
#length(x):(length(x)-250)
R> 2:(5-1)
[1] 2 3 4
These two statements are not the same and so you are not looping over the same thing.
Upvotes: 6