r - correlation loop keep getting NA -
despite using 2 complete columns every element numeric , no numbers missing rows 2 thru 570, find impossible result other na when using loop find rolling 24-week correlation between 2 columns.
rolling.correlation <- null temp <- null for(i in 2:547){ temp <- cor(training.set$return.spy[i:i+23],training.set$return.tlt[i:i+23]) rolling.correlation <- c(rolling.correlation, temp) } #end "for" loop rolling.correlation
the cor()command works fine [2:25], [i:25], or [2:i] r doesn't understand when [i:i+23]
i want r calculate correlation rows 2 thru 25, 3 thru 26, ..., 547 thru 570. result should vector of length 546 has numeric values each correlation. instead i'm getting vector of 546 nas. how can fix this? help.
look happens when
5:5+2 # [1] 7
note :
has higher operator precedence +
means 5:5+2
same (5:5)+2
when want 5:(5+2)
. use
i:(i+23)
Comments
Post a Comment