R Error in aggregate.data.frame() : arguments must have same length (Example)
On this page, I’ll demonstrate how to handle the “Error in aggregate.data.frame(as.data.frame(x), …) : arguments must have same length” in the R programming language.
The table of content looks as follows:
Let’s dive into it.
Creation of Example Data
Before all else, we’ll need to create some data that we can use in the exemplifying syntax below:
data <- data.frame(x = 1:6, # Create example data group = letters[1:3]) data # Print example data

Table 1 shows the structure of our example data – It is constituted of six rows and two columns. The variable x contains numeric values and the variable group is a grouping indicator separating our data into different subsets.
Example 1: Reproduce the Error in aggregate.data.frame – arguments must have same length
The following R programming code shows how to replicate the “Error in aggregate.data.frame(as.data.frame(x), …) : arguments must have same length” in the R programming language.
Let’s assume that we want to calculate the mean by group for the column x. Then, we might try to use the aggregate function as shown below:
data_aggr <- aggregate(data$x, list("group"), mean) # Try to apply aggregate # Error in aggregate.data.frame(as.data.frame(x), ...) : # arguments must have same length
Unfortunately, the execution of the previous R code lead to the message “Error in aggregate.data.frame(as.data.frame(x), …) : arguments must have same length”.
The reason for this error message is that we have not specified the grouping column properly within the aggregate function.
In the next example, I’ll explain how to fix this error!
Example 2: Fix the Error in aggregate.data.frame – arguments must have same length
In this example, I’ll show how to avoid the error message “Error in aggregate.data.frame(as.data.frame(x), …) : arguments must have same length”.
For this, we have to specify the data set containing the column “group” explicitly (i.e. data$group instead of “group”).
Have a look at the following R code and its output:
data_aggr <- aggregate(data$x, list(data$group), mean) # Properly apply aggregate data_aggr # Print output

The output of the previous R syntax is shown in Table 2 – We have created a data frame containing the mean values of each group in our data.
The “Error in aggregate.data.frame(as.data.frame(x), …) : arguments must have same length” did not appear this time!
Video & Further Resources
Would you like to learn more about the handling of the “Error in aggregate.data.frame(as.data.frame(x), …) : arguments must have same length”? Then I recommend having a look at the following video on my YouTube channel. I show the topics of this tutorial in the video:
The YouTube video will be added soon.
Furthermore, you might read the related articles on this homepage:
- Error in apply(data) : dim(X) must have a positive length
- Error : ‘names’ attribute must be the same length as the vector
- Errors & Warnings in R
- R Programming Language
In this R programming tutorial you have learned how to deal with the “Error in aggregate.data.frame(as.data.frame(x), …) : arguments must have same length”. Please let me know in the comments section, in case you have any additional questions.
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.







2 Comments. Leave new
I would like to have de the tutorial on error data of R.
Hey,
Thanks for the comment. Could you please be a bit more precise on what you are looking for?
Regards,
Joachim