Group By Sum in R, the `group_by()`

function is a powerful tool that allows you to split your data into groups based on specific variables or columns.

Once the data is grouped, you can perform various operations on these groups, such as calculating summary statistics or aggregating values.

One such operation is the `sum()`

function, which computes the sum of values within each group. In this explanation, we will discuss how to use `group_by()`

and `sum()`

with in-built datasets in R, along with examples.

Qualification Required for Data Scientist »

To illustrate the usage of `group_by()`

and `sum()`

, we will be working with the in-built dataset called “mtcars.”

The “mtcars” dataset is a subset of data from the 1974 book “Applied Regression Analysis and General Linear Models” by D.S. Collett.

It contains information on 32 automobiles, including their mileage, weight, horsepower, and other specifications.

First, let’s load the “mtcars” dataset into R:

`data(mtcars)`

Now, we can use the `group_by()`

function from the “dplyr” package to group the data based on a specific variable or column. The “dplyr” package is a popular set of tools for working with data frames in R and is widely used for data manipulation and transformation. To install and load the “dplyr” package, use the following commands:

```
install.packages("dplyr")
library(dplyr)
```

Suppose we want to calculate the sum of the “mpg” (miles per gallon) column for each car manufacturer in the “mtcars” dataset.

We can achieve this by grouping the data by the “cyl” column and then applying the `sum()`

function to the “mpg” column. Here’s how you would do it:

```
# Group by manufacturer and calculate the sum of mpg
mtcars_by_cyl <- mtcars %>%
+ group_by(cyl) %>%
+ summarise(total_mpg = sum(mpg))
```

In the code above, we use the pipe operator `%>%`

to pass the “mtcars” data frame to the `group_by()`

function, specifying the “cyl” column.

Then, we use the `summarise()`

function to calculate the sum of “mpg” within each group. The result is stored in the “mtcars_by_cyl” data frame.

To view the resulting data frame, you can use:

```
mtcars_by_cyl
# A tibble: 3 × 2
cyl total_mpg
<dbl> <dbl>
1 4 293.
2 6 138.
3 8 211.
```

The output will display the sum of “mpg” for each car manufacturer in the “mtcars” dataset.

Now, let’s consider another example. Suppose we want to calculate the total weight and horsepower for each car ‘am’ in the “mtcars” dataset.

We can achieve this by grouping the data by the “am” column and then applying the `sum()`

function to the “wt” (weight) and “hp” (horsepower) columns. Here’s how you would do it:

```
# Group by model and calculate the sum of wt and hp
mtcars_by_model <- mtcars %>%
+ group_by(am) %>%
+ summarise(total_wt = sum(wt), total_hp = sum(hp))
```

Similar to the previous example, we use the `group_by()`

function to group the data by the “am” column and then use the `summarise()`

function to calculate the sum of “wt” and “hp” within each group.

The result is stored in the “mtcars_by_model” data frame. To view the resulting data frame, use:

```
mtcars_by_model
# A tibble: 2 × 3
am total_wt total_hp
<dbl> <dbl> <dbl>
1 0 71.6 3045
2 1 31.3 1649
```

The output will display the total weight and horsepower for each car model in the “mtcars” dataset.

In conclusion, the `group_by()`

and `sum()`

functions in R, along with the “dplyr” package, provide a powerful way to analyze and summarize data.

By grouping data based on specific variables and calculating summary statistics or aggregating values, you can gain valuable insights from your data.

The examples provided in this explanation demonstrate how to use these functions with the in-built “mtcars” dataset in R.