R错误:替换长度为零 [英] R Error: replacement has length zero

查看:475
本文介绍了R错误:替换长度为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设我有以下几种:

  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)
[1] 1

所以,我找到正确的索引位置的代码将是错误的。然而,我仍然期待它运行。

你正在得到这个错误,因为你正在试图分配长度为零的值。把这行代码放在 print(which(cP [c((i):( length(indicator.Trade)-1))] == - 1)) code> else if() block并且看到错误来自那里。这是因为使用来获得两个数字之间的序列。在这里,你正在试图获得空值的序列,这是无效的操作。它出现在中的第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


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 zero

I 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] 1

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 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 0

This 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

这篇关于R错误:替换长度为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆