参数长度为零 - R

时间:2016-10-04 14:10:08

标签: r

我在this problem上看过很多关于SO的问题,但是我无法理解,并且与我的案例有关。我需要测试一个案例,然后采取适当的行动。显然,以下代码不是我最终会使用的代码......但它说明了问题。

代码

初始代码

threshold <- 10
slotDuration <- 10 # data arriving in slotDuration sec will get into a single slot
totalSlots <- 200 # number of slots to keep 
prevSlot <- 0

window <- slotDuration * totalSlots # window [time in sec] of data to cover
data <- array (0, dim=c(1,totalSlots))
handler <- function(transactionTime, dataValue){
    # This line perhaps does not matter as the test uses the destSlot variable below.
    dput(transactionTime)
    n <- as.numeric(transactionTime, units = "secs")

    # This is the var that causes problem
   destSlot <- ceiling((n %% window)/slotDuration)

   # this line should ensure we dont proceed on null values .. right ?
   if (is.null(destSlot)) {
      print("This is NULLL")
      return (0)
   }

   if (!is.null(destSlot)) {
       print(destSlot)       ## prints numeric(0)
       print("This is NOT NULLL")
   }

   if (destSlot != 0){ ## This line causes argument length zero error
       prevSlot <<- destSlot
       data[destSlot] <<- 0 
   }
   data[destSlot] <<- data[destSlot] + dataValue

}

输出

structure(1475591155, class = c("POSIXct", "POSIXt"))
[1] 116
[1] "This is NOT NULLL"
Error in eval(substitute(expr), envir, enclos) : 
argument is of length zero

0 个答案:

没有答案