R错误:替换长度为零(R Error: replacement has length zero)

编程入门 行业动态 更新时间:2024-10-21 13:31:57
R错误:替换长度为零(R Error: replacement has length zero)

我试图找出最近的位置来表示接近的位置(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 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.

最满意答案

你正在得到那个错误,因为你正试图分配一个长度为零的值。 把这行代码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 NA

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

更多推荐

本文发布于:2023-07-08 20:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080261.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为零   长度   错误   length   replacement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!