knitr::opts_chunk$set(echo = TRUE, collapse = TRUE, comment = "#>")
library(dapr)

dapr

Build status CRAN status Coverage Status DOI Downloads Downloads lifecycle

Dependency-free purrr-like apply/map/iterate functions

Installation

Install the development version from Github with:

## 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}, (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.

Use

Function names use the convention *ap() where * is the first letter of output data type.

Common inputs:

Vectors

Functions that apply expressions to input data objects and return atomic vectors e.g., numeric (double), character, logical.

## 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.

## list of strings
lap(e[1:2], ~ paste0(.x, "."))
## 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.

## 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))


mkearney/ifl documentation built on June 29, 2019, 7:33 a.m.