File: grouping.R

package info (click to toggle)
r-cran-dplyr 1.1.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,292 kB
  • sloc: cpp: 1,403; sh: 17; makefile: 7
file content (143 lines) | stat: -rw-r--r-- 4,244 bytes parent folder | download
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
## ----echo = FALSE, message = FALSE, warning = FALSE---------------------------
knitr::opts_chunk$set(collapse = T, comment = "#>")
options(tibble.print_min = 4L, tibble.print_max = 4L)

## ----message = FALSE----------------------------------------------------------
library(dplyr)

## -----------------------------------------------------------------------------
by_species <- starwars %>% group_by(species)
by_sex_gender <- starwars %>% group_by(sex, gender)

## -----------------------------------------------------------------------------
by_species
by_sex_gender

## -----------------------------------------------------------------------------
by_species %>% tally()

by_sex_gender %>% tally(sort = TRUE)

## ----group_by_with_expression-------------------------------------------------
bmi_breaks <- c(0, 18.5, 25, 30, Inf)

starwars %>%
  group_by(bmi_cat = cut(mass/(height/100)^2, breaks=bmi_breaks)) %>%
  tally()

## ----group_vars---------------------------------------------------------------
by_species %>% group_keys()

by_sex_gender %>% group_keys()

## -----------------------------------------------------------------------------
by_species %>% group_indices()

## -----------------------------------------------------------------------------
by_species %>% group_rows() %>% head()

## -----------------------------------------------------------------------------
by_species %>% group_vars()
by_sex_gender %>% group_vars()

## -----------------------------------------------------------------------------
by_species %>%
  group_by(homeworld) %>%
  tally()

## -----------------------------------------------------------------------------
by_species %>%
  group_by(homeworld, .add = TRUE) %>%
  tally()

## -----------------------------------------------------------------------------
by_species %>%
  ungroup() %>%
  tally()

## -----------------------------------------------------------------------------
by_sex_gender %>% 
  ungroup(sex) %>% 
  tally()

## ----summarise----------------------------------------------------------------
by_species %>%
  summarise(
    n = n(),
    height = mean(height, na.rm = TRUE)
  )

## -----------------------------------------------------------------------------
by_sex_gender %>% 
  summarise(n = n()) %>% 
  group_vars()

by_sex_gender %>% 
  summarise(n = n(), .groups = "drop_last") %>% 
  group_vars()

## -----------------------------------------------------------------------------
by_sex_gender %>% 
  summarise(n = n(), .groups = "keep") %>% 
  group_vars()

by_sex_gender %>% 
  summarise(n = n(), .groups = "drop") %>% 
  group_vars()

## ----select-------------------------------------------------------------------
by_species %>% select(mass)

## -----------------------------------------------------------------------------
by_species %>%
  arrange(desc(mass)) %>%
  relocate(species, mass)

by_species %>%
  arrange(desc(mass), .by_group = TRUE) %>%
  relocate(species, mass)

## ----by_homeworld-------------------------------------------------------------
# Subtract off global mean
starwars %>% 
  select(name, homeworld, mass) %>% 
  mutate(standard_mass = mass - mean(mass, na.rm = TRUE))

# Subtract off homeworld mean
starwars %>% 
  select(name, homeworld, mass) %>% 
  group_by(homeworld) %>% 
  mutate(standard_mass = mass - mean(mass, na.rm = TRUE))

## -----------------------------------------------------------------------------
# Overall rank
starwars %>% 
  select(name, homeworld, height) %>% 
  mutate(rank = min_rank(height))

# Rank per homeworld
starwars %>% 
  select(name, homeworld, height) %>% 
  group_by(homeworld) %>% 
  mutate(rank = min_rank(height))

## ----filter-------------------------------------------------------------------
by_species %>%
  select(name, species, height) %>% 
  filter(height == max(height))

## ----filter_group-------------------------------------------------------------
by_species %>%
  filter(n() != 1) %>% 
  tally()

## ----slice--------------------------------------------------------------------
by_species %>%
  relocate(species) %>% 
  slice(1)

## ----slice_min----------------------------------------------------------------
by_species %>%
  filter(!is.na(height)) %>% 
  slice_min(height, n = 2)