Calculate Sum by Group & Assign as New Variable to Data Frame in R (Example Code)

This article explains how to get the mean by group and assign the output as a new variable to a data frame in R.

Creation of Example Data

data(iris)                  # Loading 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

Example: Append Sum by Group as a New Variable to a Data Frame Using the dplyr Package

install.packages("dplyr")   # Install dplyr package
library("dplyr")            # Load dplyr
iris_new <- iris %>%        # Computing mean by group
  group_by(Species) %>%
  mutate(SL_sum = sum(Sepal.Length))
head(iris_new)
             # # A tibble: 6 × 6
# # Groups:   Species [1]
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species SL_sum
#          <dbl>       <dbl>        <dbl>       <dbl> <fct>    <dbl>
# 1          5.1         3.5          1.4         0.2 setosa    876.
# 2          4.9         3            1.4         0.2 setosa    876.
# 3          4.7         3.2          1.3         0.2 setosa    876.
# 4          4.6         3.1          1.5         0.2 setosa    876.
# 5          5           3.6          1.4         0.2 setosa    876.
# 6          5.4         3.9          1.7         0.4 setosa    876.

Related Articles

Have a look at the following R tutorials. They discuss topics such as dplyr, vectors, loops, and missing data.

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top