-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathREADME.Rmd
212 lines (155 loc) Β· 4.89 KB
/
README.Rmd
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# dance <img src="man/figures/logo.png" align="right" />
<!-- badges: start -->
[](https://www.tidyverse.org/lifecycle/)
[](https://travis-ci.org/romainfrancois/dance)
<!-- badges: end -->

Dancing `r emo::ji("woman_dancing")` with the stats, aka `tibble()` dancing `r emo::ji("man_dancing")`.
`dance` is a sort of reinvention of `dplyr` classic verbs, with a more modern stack
underneath, i.e. it leverages a lot from `vctrs` and `rlang`.
# Installation
You can install the development version from GitHub.
```{r, eval=FALSE}
# install.packages("pak")
pak::pkg_install("romainfrancois/dance")
```
# Usage
We'll illustrate tibble dancing with `iris` grouped by `Species`.
```{r example}
library(dance)
g <- iris %>% group_by(Species)
```
### waltz(), polka(), tango(), charleston()
These are in the neighborhood of `dplyr::summarise()`.
`waltz()` takes a grouped tibble and a list of formulas and returns a tibble with:
as many columns as supplied formulas, one row per group. It does not prepend the grouping
variables (see `tango` for that).
```{r}
g %>%
waltz(
Sepal.Length = ~mean(Sepal.Length),
Sepal.Width = ~mean(Sepal.Width)
)
```
`polka()` deals with peeling off one layer of grouping:
```{r}
g %>%
polka()
```
`tango()` binds the results of `polka()` and `waltz()` so is the closest to
`dplyr::summarise()`
```{r}
g %>%
tango(
Sepal.Length = ~mean(Sepal.Length),
Sepal.Width = ~mean(Sepal.Width)
)
```
`charleston()` is like `tango` but it packs the new columns in a tibble:
```{r}
g %>%
charleston(
Sepal.Length = ~mean(Sepal.Length),
Sepal.Width = ~mean(Sepal.Width)
)
```
### swing, twist
There is no `waltz_at()`, `tango_at()`, etc ... but instead we can use
either the same function on a set of columns or a set of functions on the same column.
For this, we need to learn new dance moves:
`swing()` and `twist()` are for applying the same function to a set
of columns:
```{r}
library(tidyselect)
g %>%
tango(swing(mean, starts_with("Petal")))
g %>%
tango(data = twist(mean, starts_with("Petal")))
```
They differ in the type of column is created and how to name them:
- `swing()` makes as many new columns as are selected by the tidy selection, and
the columns are named using a `.name` glue pattern, this way we might `swing()`
several times.
```{r}
g %>%
tango(
swing(mean, starts_with("Petal"), .name = "mean_{var}"),
swing(median, starts_with("Petal"), .name = "median_{var}"),
)
```
- `twist()` instead creates a single data frame column.
```{r}
g %>%
tango(
mean = twist(mean, starts_with("Petal")),
median = twist(median, starts_with("Petal")),
)
```
The first arguments of `swing()` and `twist()` are either a function or a
formula that uses `.` as a placeholder. Subsequent arguments are
tidyselect selections.
You can combine `swing()` and `twist()` in the same `tango()` or `waltz()`:
```{r}
g %>%
tango(
swing(mean, starts_with("Petal"), .name = "mean_{var}"),
median = twist(median, contains("."))
)
```
### rumba, zumba
Similarly `rumba()` can be used to apply several functions to a single column.
`rumba()` creates single columns and `zumba()` packs them into a data frame column.
```{r}
g %>%
tango(
rumba(Sepal.Width, mean = mean, median = median, .name = "Sepal_{fun}"),
Petal = zumba(Petal.Width, mean = mean, median = median)
)
```
### salsa, chacha, samba, madison
Now we enter the realms of `dplyr::mutate()` with:
- `salsa()` : to create new columns
- `chacha()`: to reorganize a grouped tibble so that data for each group is contiguous
- `samba()` : `chacha()` + `salsa()`
```{r}
g %>%
salsa(
Sepal = ~Sepal.Length * Sepal.Width,
Petal = ~Petal.Length * Petal.Width
)
```
You can `swing()`, `twist()`, `rumba()` and `zumba()` here too, and if you
want the original data, you can use `samba()` instead of `salsa()`:
```{r}
g %>%
samba(centered = twist(~ . - mean(.), everything(), -Species))
```
`madison()` packs the columns `salsa()` would have created
```{r}
g %>%
madison(swing(~ . - mean(.), starts_with("Sepal")))
```
### bolero and mambo
`bolero()` is similar to `dplyr::filter()`.
The formulas may be made by `mambo()` if you want to apply the same
predicate to a tidyselection of columns:
```{r}
g %>%
bolero(~Sepal.Width > 4)
g %>%
bolero(mambo(~. > 4, starts_with("Sepal")))
g %>%
bolero(mambo(~. > 4, starts_with("Sepal"), .op = or))
```