R错误:替换长度为零 [英] R Error: replacement has length zero
问题描述
假设我有以下几种:
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)
##如果指标。 [1]是1,我想获得order.book [1,1] = 1和order.book [2,1] = 3。
##如果indicator.Trade [2]是1,我想获得order.book [1,2] = 2和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 )(cP [c((i)):( length(indicator.Trade)-1)($) ))] == - 1),1)
} else {
order.book [1,i] = i
order.book [2,i] = 0
}
}
但是运行上面的代码我得到以下错误:
pre $
in order.book [2,i] = head(which(cP [c((i):( length(indicator.Trade) - :
更换长度为零
我试过用手动代替:
i = 1 and i =(length(indicator.Trade)-1)
如检查数字(0),但似乎并非如此。我在这里错过了什么? 编辑 我刚刚意识到(cP [c(((length(indicator.Trade)-1)):(长度(指标。 Trade)-1))] == 1),1) 所以,我找到正确的索引位置的代码将是错误的。然而,我仍然期待它运行。 你正在得到这个错误,因为你正在试图分配长度为零的值。把这行代码放在 试试这个小练习来看看发生了什么 这就是为什么我使用 修改后的代码 I'm trying to identify the nearest position to indicate close position(cP). Let's say I have the following: But running the code above I get the following error: I tried substituting manually: 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 So, my code for finding the correct index position would be wrong. However, I'm still expecting it to run though. You are getting that error, because you are trying to assign a value of length zero. Put this line of code Try this small exercise to see what is happening This is why I used Modified code
这篇关于R错误:替换长度为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
[1] 1
:
来获得两个数字之间的序列。在这里,你正在试图获得空值的序列,这是无效的操作。它出现在中的第12列,否则如果是
块。我也在下面的代码中添加了print语句。
<$使用`:`$ b $产生序列
#在1:a1:长度为0的参数中的错误$:$ a $< code> a1< - NULL#创建一个空变量
1: b $ b seq_len
for循环。阅读?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在(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
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)
head(which(cP[c(((length(indicator.Trade)-1)):(length(indicator.Trade)-1))]==1),1)
[1] 1
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.a1 <- NULL # create a null variable
1:a1 # generate sequence using `:`
# Error in 1:a1 : argument of length 0
seq_len
function in the for loop. Read ?seq_len
and ?seq_along
man pages.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