-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3patternsForDataAnalysis.qmd
More file actions
152 lines (94 loc) · 4.81 KB
/
Copy path3patternsForDataAnalysis.qmd
File metadata and controls
152 lines (94 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
---
title: "R_Basic_Patterns"
format: html
editor: visual
---
## 3 Common Patterns for Data Analysis Workflows
Here are 3 basic patterns using `dplyr` functions that you can use for practice. These patterns combine different verbs into common data analysis workflows.
For these examples, we will use the built-in `mtcars` dataset, which contains information about various car models. You can load it and the `dplyr` library with the following code:
```{r}
library(dplyr)
library(tibble)
data(mtcars)
summary(mtcars)
```
## **Pattern 1: Basic summary and filtering**
This is a fundamental pattern for exploring and summarizing a dataset. It involves:
1. **Filtering** rows based on a condition.
2. **Selecting** a few key columns.
3. **Grouping** the data.
4. **Summarizing** the groups.
**The task:** Find the average miles per gallon (MPG) and horsepower for 4 and 6-cylinder cars.
**The pattern:**
```{r}
mtcars %>%
filter(cyl %in% c(4, 6)) %>%
select(mpg, hp, cyl) %>%
group_by(cyl) %>%
summarise(
avg_mpg = mean(mpg),
avg_hp = mean(hp)
)
summary(mtcars)
str(mtcars)
```
**What is happening?**
- `filter(cyl %in% c(4, 6))`: Filters the dataset to include only rows where the `cyl` (number of cylinders) is either 4 or 6.
- `select(mpg, hp, cyl)`: Keeps only the columns for MPG, horsepower, and cylinders. This is a good practice to avoid carrying unnecessary columns.
- `group_by(cyl)`: Prepares the data for aggregation by `cyl`, so the next operation will be performed separately for the 4-cylinder and 6-cylinder groups.
- `summarize(...)`: Calculates the `avg_mpg` and `avg_hp` for each group, collapsing the data into a single row per group.
## Pattern 2: Creating new variables and sorting
This pattern demonstrates how to transform data by creating new columns and then ordering the results. It involves:
1. **Creating** a new column with `mutate()`.
2. **Filtering** for a specific condition on the new column.
3. **Arranging** the results.
4. **Renaming** a column for better clarity.
**The task:** Create a new column for "weight per horsepower," and find the top 5 cars with the lowest ratio for 8-cylinder engines.
**The pattern:**
```{r}
mtcars <- tibble::rownames_to_column(mtcars, var = "car_model")
mtcars %>%
mutate(weight_per_hp = wt / hp) %>%
filter(cyl == 8) %>%
arrange(weight_per_hp) %>%
select(car_model, weight_per_hp, hp) %>%
head(5)
```
**What is happening?**
The first function is to create a column out of the names of the type of car model.
- First we used the assignment operator to call the tibble library which was loaded at the top of our page. Then we used the rownames_to_column(mtcars, var = "car_model") function to convert the row of names into a column and variable we called 'car_model' from the mtcars data set.
- `mutate(weight_per_hp = wt / hp)`: Creates the new `weight_per_hp` variable.
- `filter(cyl == 8)`: Focuses the analysis on 8-cylinder cars.
- `arrange(weight_per_hp)`: Sorts the rows in ascending order based on the new variable.
- `select(...)`: Selects the new variable and other relevant columns to present in the result.
- `head(5)`: Displays only the top 5 rows of the sorted result.
**Pattern 3: Summarizing with unique values**
This is a common workflow for checking the unique combinations of categories in a dataset and then summarizing a numerical variable within those unique combinations. It involves:
1. **Grouping** by multiple variables.
2. **Summarizing** to get a count of unique combinations.
3. **Grouping** again, this time for a deeper summary.
4. **Summarizing** with another statistical function.
5. **Arranging** to display the result clearly.
**The task:** Find the number of car models for each unique combination of cylinders and gear, and then calculate the average MPG for each of those groups.
**The pattern:**
```{r}
mtcars %>%
distinct(cyl, gear) %>% # to find the unique combinations of cylinders and gears
arrange(cyl, gear) %>%
print()
mtcars %>%
group_by(cyl, gear) %>%
summarize(
model_count = n(),
avg_mpg = mean(mpg),
.groups = "drop" # Drop grouping for subsequent operations
) %>%
arrange(cyl, gear)
```
**What is happening?**
- `distinct(cyl, gear)`: The first block of code finds and prints all unique combinations of `cyl` and `gear` in the data, which can be a good way to understand the structure of your categorical variables.
- `group_by(cyl, gear)`: In the second block, it groups the data by both `cyl` and `gear` for the deeper summary.
- `summarize(...)`:
- `model_count = n()`: Uses the special `n()` function to count the number of rows (i.e., car models) within each `cyl` and `gear` group.
- `avg_mpg = mean(mpg)`: Calculates the average MPG for each group.
- `arrange(cyl, gear)`: Sorts the final table by `cyl` and `gear` for easy readability.