r - Plotting in a for loop: Why can't I subtract from the counter? -


why following code plot points @ 1:10 , not @ 0:9 on x axis? (i know can code differently solve problem, nevertheless, wish know.)

y <- rep(1,10)  (i in 1:10) {            if (i == 1) {                     plot(y[i]~(i-1),pch = 14,                          ylim = c(0,2),                          xlim=c(0,11))           }           else {points(y[i]~(i-1), pch = 14)           } } 

you have use i() (as-is) formula construct:

y <- rep(1,10) (i in 1:10) {           if (i == 1) {                     plot(y[i]~i(i-1),pch = 14,                          ylim = c(0,2),                          xlim=c(0,11))           }           else {points(y[i]~i(i-1), pch = 14)           } } 

this (according ?i) because arithmetic operators ("+", "-", "*" , "^") inside formulae interpreted formula operators (for adding/dropping terms or creating interactions) rather arithmetic operators.

you obtain same changing plot calls to: plot(i-1,y[i],...) out of formula mode.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -