我试图找出最近的位置来表示接近的位置(cP)。
假设我有以下几点:
indicator.Trade=c(1,1,0,0,-1,0,0,0,1,1,0,-1,-1) cP=c(NA,NA,1,1,NA,-1,NA,NA,1,NA,NA,1,NA) ## If indicator.Trade[1] is 1, I want to obtain order.book[1,1]=1 and order.book[2,1]=3. ## If indicator.Trade[2] is 1, I want to obtain order.book[1,2]=2 and order.book[2,2]=3. order.book=matrix(0,nrow=2,ncol=(length(indicator.Trade)-1)) for(i in 1:(length(indicator.Trade)-1)){ if( (indicator.Trade[i]==1) ){ order.book[1,i]=i order.book[2,i]=head(which(cP[c((i):(length(indicator.Trade)-1))]==1),1) } else if(indicator.Trade[i]==-1){ order.book[1,i]=i order.book[2,i]=head(which(cP[c((i):(length(indicator.Trade)-1))]==-1),1) } else { order.book[1,i]=i order.book[2,i]=0 } }但是运行上面的代码我得到以下错误:
in order.book[2, i] = head(which(cP[c((i):(length(indicator.Trade) - : replacement has length zero我尝试手动替换:
i=1 and i=(length(indicator.Trade)-1)正如R中的Simple for循环中R中所提出的“替换具有长度为零”中所建议的,以检查数字(0),但似乎并非如此。 我在这里错过了什么?
编辑
我刚刚意识到这一点
head(which(cP[c(((length(indicator.Trade)-1)):(length(indicator.Trade)-1))]==1),1) [1] 1所以,我的代码找到正确的索引位置是错误的。 不过,我仍然期待它能够运行。
I'm trying to identify the nearest position to indicate close position(cP).
Let's say I have the following:
indicator.Trade=c(1,1,0,0,-1,0,0,0,1,1,0,-1,-1) cP=c(NA,NA,1,1,NA,-1,NA,NA,1,NA,NA,1,NA) ## If indicator.Trade[1] is 1, I want to obtain order.book[1,1]=1 and order.book[2,1]=3. ## If indicator.Trade[2] is 1, I want to obtain order.book[1,2]=2 and order.book[2,2]=3. order.book=matrix(0,nrow=2,ncol=(length(indicator.Trade)-1)) for(i in 1:(length(indicator.Trade)-1)){ if( (indicator.Trade[i]==1) ){ order.book[1,i]=i order.book[2,i]=head(which(cP[c((i):(length(indicator.Trade)-1))]==1),1) } else if(indicator.Trade[i]==-1){ order.book[1,i]=i order.book[2,i]=head(which(cP[c((i):(length(indicator.Trade)-1))]==-1),1) } else { order.book[1,i]=i order.book[2,i]=0 } }But running the code above I get the following error:
in order.book[2, i] = head(which(cP[c((i):(length(indicator.Trade) - : replacement has length zeroI tried substituting manually:
i=1 and i=(length(indicator.Trade)-1)As suggested in Simple for loop in R producing "replacement has length zero" in R to check for numeric(0) but that doesn't seem to be the case. What am I missing here?
Edit
I've just realized that
head(which(cP[c(((length(indicator.Trade)-1)):(length(indicator.Trade)-1))]==1),1) [1] 1So, my code for finding the correct index position would be wrong. However, I'm still expecting it to run though.
最满意答案
你正在得到那个错误,因为你正试图分配一个长度为零的值。 把这行代码print(which(cP[c((i):(length(indicator.Trade)-1))]==-1)) , else if()阻塞,并看到错误来自那里。 这是因为:用于在两个数字之间获得序列。 在这里,您正在尝试使用空值来获取序列,这是无效的操作。 它发生在else if块的第12列。 我在下面的代码中添加了print语句。
试试这个小练习来看看发生了什么
a1 <- NULL # create a null variable 1:a1 # generate sequence using `:` # Error in 1:a1 : argument of length 0这就是为什么我在for循环中使用seq_len函数的原因。 阅读?seq_len和?seq_along手册页。
修改后的代码
indicator.Trade=c(1,1,0,0,-1,0,0,0,1,1,0,-1,-1) cP=c(NA,NA,1,1,NA,-1,NA,NA,1,NA,NA,1,NA) len_ind_tr <- length(indicator.Trade) order.book <- matrix(0,nrow=2,ncol=len_ind_tr-1)) for(i in seq_len(len_ind_tr-1)){ if(indicator.Trade[i] == 1){ order.book[1,i] <- i order.book[2,i] <- which(cP[i:(len_ind_tr-1)] == 1)[1] } else if(indicator.Trade[i] == -1){ order.book[1,i] <- i order.book[2,i] <- which(cP[i:(len_ind_tr-1)] == -1)[1] print(which(cP[i:(len_ind_tr-1)] == -1)) } else { order.book[1,i] <- i order.book[2,i] <- 0 } } order.book # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] # [1,] 1 2 3 4 5 6 7 8 9 10 11 12 # [2,] 3 2 0 0 2 0 0 0 1 3 0 NAYou are getting that error, because you are trying to assign a value of length zero. Put this line of code print(which(cP[c((i):(length(indicator.Trade)-1))]==-1)) inside else if() block and see the error comes from there. It is because : is used to get sequence between two numbers. Here you are trying to get the sequence with null value, which is invalid operation. It occurs for column 12 in the else if block. I added the print statement in the code below as well.
Try this small exercise to see what is happening
a1 <- NULL # create a null variable 1:a1 # generate sequence using `:` # Error in 1:a1 : argument of length 0This is why I used seq_len function in the for loop. Read ?seq_len and ?seq_along man pages.
Modified code
indicator.Trade=c(1,1,0,0,-1,0,0,0,1,1,0,-1,-1) cP=c(NA,NA,1,1,NA,-1,NA,NA,1,NA,NA,1,NA) len_ind_tr <- length(indicator.Trade) order.book <- matrix(0,nrow=2,ncol=len_ind_tr-1)) for(i in seq_len(len_ind_tr-1)){ if(indicator.Trade[i] == 1){ order.book[1,i] <- i order.book[2,i] <- which(cP[i:(len_ind_tr-1)] == 1)[1] } else if(indicator.Trade[i] == -1){ order.book[1,i] <- i order.book[2,i] <- which(cP[i:(len_ind_tr-1)] == -1)[1] print(which(cP[i:(len_ind_tr-1)] == -1)) } else { order.book[1,i] <- i order.book[2,i] <- 0 } } order.book # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] # [1,] 1 2 3 4 5 6 7 8 9 10 11 12 # [2,] 3 2 0 0 2 0 0 0 1 3 0 NA更多推荐
发布评论