How to Solve R Error: Arguments imply differing number of rows

by | Programming, R, Tips

This error occurs when you try to create a data frame with vectors with different lengths. The resultant data frame would have a differing number of rows in each column.

You can solve the error by checking the lengths of the vectors and filling the shorter vectors with NA to match the length of the longest vector.

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


Table of contents

Example

Let’s look at an example of creating a data frame with two vectors.

# Define two vectors
x <- c(1, 3, 5, 7, 9, 11)
y <- c(2, 4, 6, 8, 10, 12 , 14)

# Create data frame using vectors as columns
df <- data.frame(x, y)

Let’s run the code to see what happens:

Error in data.frame(x, y) : 
  arguments imply differing number of rows: 6, 7

The error occurs because the vector x does not have the same length as the vector y. Therefore, the resultant data frame would not have the same number of rows for each column, which is not allowed in R.

We can check the length of a vector using the length() function. Let’s print the lengths of the two vectors.

print(length(x))
print(length(y))
6
7

Solution

We can see that the vector x has a length of 6 and the vector y has a length of 7.

We can solve the error by ensuring each vector has the same length. We can use an if-statement to compare the lengths of the two vectors and pad the shortest vector with NA values.

Let’s look at the solution code:

# Define two vectors
x <- c(1, 3, 5, 7, 9, 11)
y <- c(2, 4, 6, 8, 10, 12 , 14)

# If statement comparing the lengths of two vectors

if (length(x) < length(y)){

    length(x) <- length(y)

} else if (length(y) < length(x)){

    length(y)<-length(x)

}

df <- data.frame(x, y)

print(df)

Let’s run the code to get the result:

   x  y
1  1  2
2  3  4
3  5  6
4  7  8
5  9 10
6 11 12
7 NA 14

We successfully created the data frame using the two vectors, and we can see that the shorter vector x has a single NA value to pad it to the same length as the vector y.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on R related errors, go to the articles: 

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

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!