Registration for R in 3 Months opens August 21. Get notified →

Fundamentals of R

# summarize()

### Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

View code shown in video
``````# Load Packages -----------------------------------------------------------

library(tidyverse)

# Import Data -------------------------------------------------------------

# summarize() -------------------------------------------------------------

# With summarize(), we can go from a complete dataset down to a summary.

# We use any of the summary functions with summarize().
# Here's how we calculate the mean bill length.

penguins |>
summarize(mean_bill_length = mean(bill_length_mm))

# This doesn't work! Notice what the result is.

# We need to add na.rm = TRUE to tell R to drop NA values.

penguins |>
summarize(mean_bill_length = mean(bill_length_mm, na.rm = TRUE))

# Another option is to drop NA values before calling summarize().

penguins |>
drop_na(bill_length_mm) |>
summarize(mean_bill_length = mean(bill_length_mm))

# We can have multiple arguments in each usage of summarize().

penguins |>
summarize(mean_bill_length = mean(bill_length_mm, na.rm = TRUE),
max_bill_depth = max(bill_depth_mm, na.rm = TRUE))``````

``````# Load Packages -----------------------------------------------------------

library(tidyverse)

# Import Data -------------------------------------------------------------

# Copy the data into the RStudio project
# Create a new R script file and add code to import your data

# Calculate the weight of the heaviest penguin.
# Don't forget to drop NAs!

# Calculate the minimum and maximum weight of penguins in the dataset.

To learn more about the `summarize()` function, check out Chapter 3 of R for Data Science.

## Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

#### Brian Slattery • September 19, 2023

I'm just curious, so feel free to ignore if this is covered later. But, you mentioned that piping sequential summarizes into each other doesn't work to get a single table with multiple columns. Is there a way to do that? I didn't know if mutate would be able to handle taking in a tibble from summarize? I was guessing there must be some other way to combine tibbles? For example, if you were getting the mean bill length from the penguins data, but also wanted to get a mean bill length from some other bird dataset, and have these in the same table side by side (I googled it and it looked like there's a merge() function, but I didn't know if that was the best way to go about it in this case)

#### Gracielle Higino Coach • September 20, 2023

Hi Brian! Thank you for your question, that's very interesting! We'll discuss this in our live session this week, stay tuned!

Perfect

#### Brian Slattery • September 19, 2023

Is there some way to change the default behavior of summarize so that it ignores NAs without having to specify it specifically? I didn't know if there was something like a global variable that you can set in the R script file, or something within the RStudio environment or installed package?

#### Gracielle Higino Coach • September 20, 2023

Hey! =D The short answer is: you shouldn't do that! There are some complicated workarounds, but by default, you should make it explicit in your code when NAs are being ignored/dropped. We'll discuss this on Thursday too!

#### Rachel Udow • March 17, 2024

1. Why is it required to use the summarize() function before using the more specific summary functions (e.g., mean())?

2. Does the "rm" in "na.rm" stand for anything? Just asking as it might help me remember that argument if so.

Thank you!

#### Libby Heeren Coach • March 17, 2024

Hey, Rachel! Here are some answers for you:

Q: Why is it required to use the summarize() function before using the more specific summary functions (e.g., mean())?

A: summarize() is allowing you to create/name a column in a data frame or tibble that will contain your summary value (like your mean). You could run mean directly on your column and it would output just a value, not a data frame or tibble with a named column.

For example, if you ran:

``````tomatoes |>
drop_na(productionvalue) |>
pull(productionvalue) |>
mean()
``````

You'd get a single value as your output:

``````[1] 3816933
``````

If you use summarize, like this:

``````tomatoes |>
drop_na(productionvalue) |>
summarize(meanvalue = mean(productionvalue))
``````

You'd get a tibble that looks like this:

``````# A tibble: 1 × 1
meanvalue
<dbl>
1  3816933.
``````

Of course, it's more helpful when you're adding a mean column to a grouped data set so that your resulting data frame has a row for each group containing the group name and the group's mean. I hope this helps!

Q: Does the "rm" in "na.rm" stand for anything? Just asking as it might help me remember that argument if so.

A: Yep! It stands for "remove" so you can say "na remove" in your mind to help remember it.

#### Rachel Udow • March 18, 2024

Thanks Libby, this is really helpful!

#### Michelle Brodesky • March 20, 2024

I was curious about Rachel's Q1 as well! Thanks for asking, Rachel, and for your response, Libby.

#### Maria Montenegro • April 1, 2024

I am not sure why I am not getting the same output as in the video. When I ran the exact same code as in the "solutins" for the first excercise I see this in the console but for all variables:

\$year \$year\$variableType Variable type: numeric \$year\$countMissing Number of missing obs.: 0 (0 %) \$year\$uniqueValues Number of unique values: 3 \$year\$centralValue Median: 2008 \$year\$quartiles 1st and 3rd quartiles: 2007; 2009 \$year\$minMax Min. and max.: 2007; 2009

Any idea why?

#### David Keyes Founder • April 2, 2024

Hmm, hard to say. Can you please post your full code so I can run it and check what might be going on?

#### Libby Heeren Coach • April 2, 2024

Hey, Maria! Just wanted to contribute my experience here: sometimes this happens to me when I use summarize and I don't know why, but if I restart R (Session > Restart R) it goes away and works fine! When in doubt, restart R 😅 Let us know if it works and share your code so we can see what you were running!

#### Maria Montenegro • April 8, 2024

Thank you! I found out what the issue was... I changed it to summarise() and it worked! so strange...

#### Keith Karani • April 30, 2024

For this question I gave my solution as this. Is this a good practice to write R code like this or should switch to the solution given in the solution?

# Calculate the minimum and maximum weight of penguins in the dataset.

wght_min_max <- penguins %>% drop_na(body_mass_g) %>% summarize(max_body_weight = max(body_mass_g), min(body_mass_g))

View(wght_min_max)

#### David Keyes Founder • April 30, 2024

That's a perfectly good way to write code!

thank you

34 Lessons