Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Summarizing and Grouping Data | Section
Practical Data Preparation in R with Tidyverse
Seção 1. Capítulo 4
single

single

Summarizing and Grouping Data

Deslize para mostrar o menu

When working with data, you often need to calculate summary statistics - like averages, counts, or totals - for different groups within your dataset. The group_by and summarize functions in the Tidyverse are essential tools for these tasks. The group_by function allows you to specify one or more columns to define groups in your data. Once your data is grouped, you can use summarize to perform calculations within each group, such as finding the mean, sum, or count. This approach helps you gain insights into patterns or differences across categories, such as comparing average sales by region or counting the number of entries per department.

123456789101112131415161718
library(dplyr) options(crayon.enabled = FALSE) # Example data frame data <- data.frame( department = c("HR", "Finance", "HR", "IT", "Finance", "IT", "HR"), salary = c(50000, 60000, 52000, 70000, 61000, 72000, 51000) ) # Calculating mean salary and count of employees by department summary_stats <- data %>% group_by(department) %>% summarize( mean_salary = mean(salary), employee_count = n() ) print(summary_stats)

After performing grouped operations, your data retains its grouping structure. This can lead to unexpected results if you continue manipulating the data without first removing the grouping. The ungroup function is important because it clears groupings, returning your data to a regular, ungrouped state. This ensures that subsequent operations are performed on the entire dataset rather than within the previously defined groups. Always consider whether you need to use ungroup after summarizing, especially before further transformations or analyses.

1234567891011121314151617181920212223
library(dplyr) options(crayon.enabled = FALSE) # Example data frame data <- data.frame( department = c("HR", "Finance", "HR", "IT", "Finance", "IT", "HR"), salary = c(50000, 60000, 52000, 70000, 61000, 72000, 51000) ) # Calculating mean salary by department (grouped) summary_stats <- data %>% group_by(department) %>% summarize(mean_salary = mean(salary)) # Removing grouping with ungroup summary_stats_ungrouped <- summary_stats %>% ungroup() # Adding a new column showing overall mean salary (not by department) overall_mean <- mean(data$salary) summary_stats_ungrouped$overall_mean <- overall_mean print(summary_stats_ungrouped)
Tarefa

Deslize para começar a programar

Use the provided students data frame to calculate summary statistics by class and add an overall minimum test score column. Follow these steps:

  • Group the students data frame by the class column;
  • Use the summarize function to calculate the average test score mean and the maximum max test score for each class;
  • Remove the grouping using the ungroup function;
  • Add a new column to your summary showing the overall minimum min test score across all classes;
  • Assign your final result to a variable named class_summary.

This task helps you practice grouping, summarizing, ungrouping, and adding summary columns in the context of student test scores.

Solução

question mark

What is the primary purpose of using group_by in data summarization with the Tidyverse?

Selecione a resposta correta

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 4
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt