Error in aggregate.data.frame – arguments must have same length in R (2 Examples)
In this tutorial you’ll learn how to solve the “Error in aggregate.data.frame(as.data.frame(x), …) : arguments must have same length” in the R programming language.
Creating Example Data
data(iris) # Load example data head(iris) # Head of example data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Load example data head(iris) # Head of example data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
Example 1: Replicating the Error in aggregate.data.frame – arguments must have same length
aggregate(iris$Sepal.Length, list("Species"), sum) # Using aggregate function wrong # Error in aggregate.data.frame(as.data.frame(x), ...) : # arguments must have same length |
aggregate(iris$Sepal.Length, list("Species"), sum) # Using aggregate function wrong # Error in aggregate.data.frame(as.data.frame(x), ...) : # arguments must have same length
Example 2: Solving the Error in aggregate.data.frame – arguments must have same length
aggregate(iris$Sepal.Length, list(iris$Species), sum) # Using aggregate function correct # Group.1 x # 1 setosa 250.3 # 2 versicolor 296.8 # 3 virginica 329.4 |
aggregate(iris$Sepal.Length, list(iris$Species), sum) # Using aggregate function correct # Group.1 x # 1 setosa 250.3 # 2 versicolor 296.8 # 3 virginica 329.4