Change Variable Names of Aggregated Data in R (Example Code)
In this tutorial you’ll learn how to change the variable names of an aggregated data frame in the R programming language.
Example Data
data(iris) # Load example data head(iris) # 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) # 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
iris_aggr1 <- aggregate(list(iris$Sepal.Length), # Apply aggregate list(iris$Species), mean) iris_aggr1 # Default column names # Group.1 c.5.1..4.9..4.7..4.6..5..5.4..4.6..5..4.4..4.9..5.4..4.8..4.8.. # 1 setosa 5.006 # 2 versicolor 5.936 # 3 virginica 6.588 |
iris_aggr1 <- aggregate(list(iris$Sepal.Length), # Apply aggregate list(iris$Species), mean) iris_aggr1 # Default column names # Group.1 c.5.1..4.9..4.7..4.6..5..5.4..4.6..5..4.4..4.9..5.4..4.8..4.8.. # 1 setosa 5.006 # 2 versicolor 5.936 # 3 virginica 6.588
Example: Set Variable Names within aggregate() Function
iris_aggr2 <- aggregate(list(values = iris$Sepal.Length), # Set names list(group = iris$Species), mean) iris_aggr2 # Aggregated data with names # group values # 1 setosa 5.006 # 2 versicolor 5.936 # 3 virginica 6.588 |
iris_aggr2 <- aggregate(list(values = iris$Sepal.Length), # Set names list(group = iris$Species), mean) iris_aggr2 # Aggregated data with names # group values # 1 setosa 5.006 # 2 versicolor 5.936 # 3 virginica 6.588
Related Tutorials & Further Resources
In addition, you may want to read the related tutorials which I have published on this homepage.