 # Error in while (t_time[length(t_time)] <= 0.1) { : argument is of length zero

Hi I am trying to create a uniform distribution which is Uniform(0, 0.1) which takes a value lambda. I am getting an error message which I believe to do with my loop structure, which says"Error in while (t_time[length(t_time)] <= 0.1) { :

Why dont you' try to do it outside a function first, you will see there are multiple mistakes. You define t_time as an empty vector, then try and query it in `while`, this raises the error. Also the first argument of runif is the number of values you want. Then you try to assign this vector to a single value of a vector. Then you use the last value of cumsum, which is just sum. Then you add the vector t_time to the scaler count. !??!???!??!!!

The problem is that

``````t_time[length(t_time)]
``````

returns NULL when you first reach the while() statement. Perhaps you want to use repeat() with a break statement.

Also:

1. Vectors in R are indexed from one, so starting count at zero may cause problems with statements like
``````y_time[count]
``````
1. arrivals is a vector, so you cannot assign it to an element of y_time as you did in
`````` y_time[count] = arrivals
``````
1. t_time seems to be intended to have several elements. Doing
``````count = count +  t_time
``````

Will add count to every element of t_time and store the result in count, making count a multi-element vector. Is that what you want?

``````t_time <- c()
length(t_time) #0
t_time #NULL

t_time[length(t_time)]  #NULL
t_time[length(t_time)]  < 0 #logical(0)

if(TRUE){cat('foo')} # 'foo'
if(logical(0)){cat('bar')} # argument is of length zero
``````

A conditional like `if` or `while` needs a logical vector of length 1 to operate on. Basically, it needs a `TRUE` or a `FALSE`, and `logical(0)` is neither of those.

Is it possible to show me this in my code? Many Thanks

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.