Fixing the error
We can fix this error by simply taking care of the case that may access the value that doesn’t exist during the iteration.
Example:
R
# Initializing a vector vect = c (5, 8, 4, 12, 15) # Iterate over the vector for (i in 2 : length (vect)) { # Assign sum vect[i] = vect[i] + vect[i - 1] # Print the value print (vect[i]) } |
Output:
How to Fix in R: Replacement has length zero
In this article, we will discuss how to fix the error of replacement has length zero in the R programming language.