-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
138 lines (101 loc) Β· 4.37 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE, comment = "#>")
library(dapr)
```
# dapr <img src="man/figures/logo.png" width="160px" align="right" />
[![Build status](https://travis-ci.org/mkearney/dapr.svg?branch=master)](https://travis-ci.org/mkearney/dapr)
[![CRAN status](https://www.r-pkg.org/badges/version/dapr)](https://cran.r-project.org/package=dapr)
[![Coverage Status](https://codecov.io/gh/mkearney/dapr/branch/master/graph/badge.svg)](https://codecov.io/gh/mkearney/dapr?branch=master)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.2528504.svg)](https://doi.org/10.5281/zenodo.2528504)
![Downloads](https://cranlogs.r-pkg.org/badges/dapr?color=yellowgreen)
![Downloads](https://cranlogs.r-pkg.org/badges/grand-total/dapr?color=dd69b4)
[![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
Dependency-free purrr-like apply/map/iterate functions
## Installation
Install the development version from Github with:
``` r
## install remotes pkg if not already
if (!requireNamespace("remotes", quietly = TRUE)) {
install.packages("remotes")
}
## install from github
remotes::install_github("mkearney/dapr")
```
## {dapr} vs. {base} & {purrr}?
**{dapr}** provides the ease and consistency of [**{purrr}**](https://purrr.tidyverse.org),
(see also: simple benchmark results plot below) including use of `~` and `.x`,
without all the dependencies. In other words, use **{dapr}** when you want a
purrr-like experience but you need a lightweight solution.
<p align="center">
<img src="tools/readme/benchmark.png"/>
</p>
## Use
Function names use the convention `*ap()` where **`*`** is the first letter of output data type.
+ <code><span style="font-weight:bold;text-decoration:underline">v</span>ap</code> for **vectors**
+ <code><span style="font-weight:bold;text-decoration:underline">l</span>ap</code> for **lists**
+ <code><span style="font-weight:bold;text-decoration:underline">d</span>ap</code> for **data frames**
Common inputs:
+ `.data` Input objectβnumeric, character, list, data frame, etc.βover which elements will be iterated. If matrix or data frame, each column will be treated as the elements which are to be iterated over.
+ `.f` Function to apply to each element of input object. This can be written as a single function name e.g., `mean`, a formula-like function call where `.x` is assumed to be the iterated over element of input data e.g., `~ mean(.x)`, or an in-line function definition e.g., `function(x) mean(x)`.
### Vectors
Functions that apply expressions to input data objects and return atomic vectors
e.g., numeric (double), character, logical.
+ **`vap_dbl()`** Iterate and return **numeric** vector.
+ **`vap_int()`** Iterate and return **integer** vector.
+ **`vap_lgl()`** Iterate and return **logical** vector.
+ **`vap_chr()`** Iterate and return **character** vector.
```{r}
## create data
set.seed(2018)
d <- replicate(5, rnorm(10), simplify = FALSE)
e <- replicate(5, sample(letters, 10), simplify = FALSE)
## numeric
vap_dbl(d, ~ mean(.x))
## integer
vap_int(d, length)
## logical
vap_lgl(d, ~ max(.x) > 3)
## character
vap_chr(e, paste, collapse = "")
```
### Lists
Function(s) that apply expressions to input data objects and return lists.
+ **`lap()`** Iterate and return a **list** vector.
```{r}
## list of strings
lap(e[1:2], ~ paste0(.x, "."))
```
+ **`ilap()`** Iterate over sequence length `.i` (instead of `.x`) and return a **list** vector.
```{r}
## list of strings
ilap(1:4, ~ paste0(letters[.i], rev(LETTERS)[.i]))
```
### Data frames
Functions that apply expressions to input data objects and return data frames.
+ **`dap*()`** Iterate and return a **data frame**
- **`dapc()`** Iterate over **columns**
- **`dapr()`** Iterate over **rows**
+ **`dap*_if()`** Conditionally iterate
- **`dapc_if()`** Conditionally iterate over **columns**
- **`dapr_if()`** Conditionally iterate over **rows**
```{r}
## some data
d <- data.frame(
a = letters[1:3],
b = rnorm(3),
c = rnorm(3),
stringsAsFactors = FALSE
)
## column explicit (same as dap)
dapc(d[-1], ~ round(.x, 2))
## rows
dapr(d[-1], round, 3)
## conditional COLUMNS
dapc_if(d, is.numeric, ~ round(.x, 4))
## conditional ROWS
dapr_if(d[-1], ~ sum(.x) >= -.7, ~ round(.x, 0))
```