Aggregate Data Frame Rows by Range of Values in R (Example Code)
In this tutorial, I’ll illustrate how to aggregate data by a certain range in the R programming language.
Example Data
data(iris) # Load iris data frame 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 iris data frame 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
Example: Aggregate Rows of Data Frame by Range of Values
install.packages("dplyr") # Install & load dplyr package library("dplyr") |
install.packages("dplyr") # Install & load dplyr package library("dplyr")
iris %>% # Group by values in interval range mutate(cuts = cut(Sepal.Length, c(- Inf, 5, 6, 7, Inf))) %>% group_by(cuts) %>% dplyr::summarize(sums = mean(Sepal.Width)) # # A tibble: 4 × 2 # cuts sums # <fct> <dbl> # 1 (-Inf,5] 3.09 # 2 (5,6] 3.11 # 3 (6,7] 2.96 # 4 (7, Inf] 3.12 |
iris %>% # Group by values in interval range mutate(cuts = cut(Sepal.Length, c(- Inf, 5, 6, 7, Inf))) %>% group_by(cuts) %>% dplyr::summarize(sums = mean(Sepal.Width)) # # A tibble: 4 × 2 # cuts sums # <fct> <dbl> # 1 (-Inf,5] 3.09 # 2 (5,6] 3.11 # 3 (6,7] 2.96 # 4 (7, Inf] 3.12
Further Resources & Related Articles
Please find some related R tutorials on topics such as extracting data, variables, and groups in the list below: