My simple code is:
library(kernlab)
zoo <- matrix(sample(1:10, 240*7200, replace = TRUE), 240, 7200)
mx=data.frame(matrix(ncol = 2, nrow =30))
mx[1,1]=0
mx[1,2]=1
m=0
mm=1
for (i in 1:30){
m=m+length(zoo[,1])
mx[i+1,1]=m
mm=mm+length(zoo[,1])
mx[i+1,2]=mm
}
Above code is first part to create mx
.
ram=data.frame(matrix(ncol = 30, nrow = length(zoo[,1])))
for (i in 1:30){
A<-zoo[,c(mx[i,2]:mx[i,1]+length(zoo[,1]))]
sc <- specc(A,data=kernelMatrix,centers=2)
ram[,i]=data.matrix(sc@.Data)
}
This parts gives
Error in zoo[, c(mx[i, 2]:mx[i, 1] + length(zoo[, 1]))] :
subscript out of bounds
if I run it from i in 1:29
, there is no error. Thus the problem is in i=30
. However, if I manually plug values of mx[30,2]
and mx[30,1]
, again there is no error. I did not understand the problem and could not solve. Can anyone suggest a solution?
Note: dim(zoo)=240 7200
Note: A<-zoo[,c(mx[30,2]:mx[30,1]+length(zoo[,1]))]
gives same error. mx[30,2]= 6961 mx[30,1]=7200. Then A<-zoo[,c(6961:7200)]
works without error.