ప్రధాన కంటెంట్‌కు వెళ్లండి
eLearner.app
మాడ్యూల్ 4 · 2లో పాఠం 2కోర్సులో 8/10~15 min
మాడ్యూల్ పాఠాలు (2/2)

ఉత్పరివర్తనలు మరియు అగ్రిగేషన్లు

Beyond selecting and filtering, dplyr offers powerful capabilities to create new calculated columns and summarize data by aggregating it into groups.


Creating New Columns: mutate()

The mutate() verb allows you to calculate new columns or modify existing ones, while keeping all original columns intact.

Code
# Adds a 'double_salary' column computed as 'salary' multiplied by 2
df %>%
  mutate(double_salary = salary * 2)

# You can create multiple columns at once
df %>%
  mutate(
    total_cost = quantity * price,
    tax = total_cost * 0.22
  )

Grouping and Aggregating: group_by() and summarise()

Often, data analysis requires calculating summary statistics (e.g. mean, sum, count) for specific groups of rows (for instance, calculating the average salary for each department).

1. group_by()

Groups the data frame by the values of one or more columns. By itself, it does not change the visual appearance of the data, but it signals to dplyr that subsequent operations should be executed "group by group".

2. summarise() (or summarize())

Collapses many rows into a single summary row by applying statistical functions like mean(), sum(), min(), max(), or n() (which counts rows).

Code
# Calculates the average salary per department
df %>%
  group_by(department) %>%
  summarise(mean_salary = mean(salary))

If you want to count how many records belong to each group:

Code
# Counts how many employees are in each city
df %>%
  group_by(city) %>%
  summarise(count = n())

Try it yourself

Exercise 1: Create a calculated column

వ్యాయామం#r.m4.l2.e1
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Given the data frame df, create a new column called total_price by multiplying the quantity column by the price column using mutate(). Save the result in df_new.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Use mutate(total_price = quantity * price) inside a pipeline on df.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Exercise 2: Group and summarize with the mean

వ్యాయామం#r.m4.l2.e2
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Calculate the mean of the salary column, grouping the records by the department column. Save the aggregated column with the name mean_salary and assign the final result to df_grouped.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Usa df %>% group_by(department) %>% summarise(mean_salary = mean(salary))

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Exercise 3: Count elements per group

వ్యాయామం#r.m4.l2.e3
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Count the number of rows for each city (city column). Assign the count to the count variable inside summarise() using the n() function, and save the final result in df_counts.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Combina group_by(city) con summarise(count = n()) in una pipeline.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Exercise 4: Multiple summaries

వ్యాయామం#r.m4.l2.e4
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Calculate the minimum value (min_price) and maximum value (max_price) of the price column grouping by the category column. Save the result in df_summary.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Inside summarise(), separate the min_price = min(price) and max_price = max(price) metrics with a comma.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Exercise 5: Grouping and combined filters

వ్యాయామం#r.m4.l2.e5
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Write a pipeline that groups df by the department column, calculates the mean of salary saving it in the new column avg_salary using mutate() (Note: use mutate, not summarise, to keep all rows), and finally filters to keep only records with salary strictly greater than avg_salary. Save in res.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Usa df %>% group_by(department) %>% mutate(avg_salary = mean(salary)) %>% filter(salary > avg_salary)

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది