How to Solve R Error in rep(1, n) : invalid “times” argument

by | Programming, R, Tips

This error occurs when you call the rep function to replicate data, but the value you provide to replicate the number is not a positive real number. You can solve this problem by ensuring that the number is a single, positive real number.

This tutorial will go through the error and how to solve it with code examples.


The rep() Function in R

The rep function replicates elements of vectors and lists. The times parameter of the function needs to be a non-negative number.

Example #1: Using a negative number as the times argument in rep

Consider the following example of using the rep() function to replicate the number 1 four times.

val <- rep(1, 4)
val

Let’s run the code to see what happens:

1 1 1 1

We successfully retrieved a numeric vector of length 4 consisting of the number 1.

Let’s try to use a negative number for the times parameter.

val <- rep(1, -4)
val

Let’s run the code to see the result:

Error in rep(1, -4) : invalid 'times' argument

The error occurs because we set the times argument to a negative number when it needs to be a non-negative value only.

Solution

We can solve the error by using a positive value for the times argument. Let’s look at the revised code:

val <- rep(1, 4)
val

Let’s run the code to see the result:

[1] 1 1 1 1

We successfully retrieved a numeric vector of length 4 consisting of the number 1.

Example #2: Using NA as the times argument in rep

The error can also occur if we use a NA value as the times argument. Consider the following example:

val <- rep(1, NA)
val

Let’s run the code to see the result:

Error in rep(1, NA) : invalid 'times' argument

Solution

We can solve this error by replacing the NA value with a valid non-negative number.

val <- rep(1,5)
val

Let’s run the code to see the result:

[1] 1 1 1 1 1

We successfully retrieved a numeric vector of length 5 consisting of the number 1.

Summary

Congratulations on reading to the end of this tutorial! If you encounter this error when passing a variable as the times argument in the rep function, check if the value is negative or NA beforehand, then replace it with a valid non-negative number.

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

For further reading on data analysis with R, go to the article: How to Download and Plot Stock Prices with quantmod in R

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!