-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
156 lines (118 loc) · 3.31 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
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- 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%",
eval = TRUE
)
devtools::load_all()
```
# wrang
<!-- badges: start -->
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=valid)
<!-- badges: end -->
## Installation
You can install the development version of from [GitHub](https://github.com/) with:
```{r eval=FALSE}
# install.packages("remotes")
remotes::install_github("rappster/wrang")
```
## What?
Functionality for programmatic data wrangling based on [{dplyr}](https://github.com/tidyverse/dplyr), [{rlang}](https://github.com/r-lib/rlang) and friends
## Why?
In non-interactive use cases you typically need more programmatic approaches than hard-coding certain input for data wrangling tasks - e.g. column names or functions to be executed.
This package tries to come up with some out-of-the-box helpers for that.
### DISCLAIMER
It's one of those scratch-your-own-itch things where the code base might change as flesh my ideas out.
## How?
```{r, eval=FALSE}
library(wrang)
```
## Frequency table
```{r}
mtcars %>% summa_freq_table(cyl)
mtcars %>% summa_freq_table(cyl, gear, .sort = TRUE)
mtcars %>% summa_freq_table(cyl, .col_n_abs = "n",
.col_n_rel = rel, .sort = TRUE)
mtcars %>% summa_freq_table(cyl, "gear")
cyl_ <- dplyr::quo(cyl)
gear_ <- dplyr::sym("gear")
mtcars %>% summa_freq_table(!!cyl_, !!gear_)
```
With specifying an "dependend variable" (not satisfied with the name yet, likely to change)
```{r}
# install.packages("palmerpenguins")
library(palmerpenguins)
penguins %>% summa_freq_table(species, island, sex, .digits_n_rel = 2)
```
Check that relative counts add up to `1`
```{r}
penguins %>%
summa_freq_table(species, island, sex,
.digits_n_rel = 2, .ungroup = FALSE) %>%
dplyr::summarise(n_rel_total = sum(n_rel)) %>%
dplyr::ungroup()
```
## Handling NSE input
```{r}
foo <- function(
data,
col = "carb_sum",
col_src = "carb",
fn = purrr::partial(sum, na.rm = TRUE)
) {
col <- dplyr::enquo(col) %>% handle_nse_input()
col_src <- dplyr::enquo(col_src) %>% handle_nse_input()
data %>% dplyr::summarize(
!!col := fn(!!col_src)
)
}
```
```{r}
mtcars %>% foo()
```
```{r}
mtcars %>% foo(
col = "mpg_mean",
col_src = "mpg",
fn = purrr::partial(mean, na.rm = TRUE)
)
```
```{r}
mtcars %>% foo(
col = mpg_mean_2,
col_src = "mpg",
fn = purrr::partial(mean, na.rm = TRUE)
)
```
```{r}
mtcars %>% foo(
col = mpg_mean_2,
col_src = mpg,
fn = purrr::partial(mean, na.rm = TRUE)
)
```
## Other
```{r}
mtcars %>% dplyr::summarize(
carb_sum = sum(carb, na.rm = TRUE),
carb_mean = mean(carb, na.rm = TRUE)
)
```
```{r}
col_sum <- "carb_sum" %>% dplyr::sym()
col_mean <- "carb_mean" %>% dplyr::sym()
col_src <- "carb" %>% dplyr::sym()
mtcars %>% dplyr::summarize(
!!col_sum := sum(!!col_src, na.rm = TRUE),
!!col_mean := mean(!!col_src, na.rm = TRUE)
)
```