# Tools for Data Manipulation

#### 2018-01-19

An implementation of common higher order functions with syntactic sugar for anonymous function. Provides also a link to ‘dplyr’ for common transformations on data frames to work around non standard evaluation by default.

## Installation

### From GitHub

devtools::install_github("wahani/dat")

### From CRAN

install.packages("dat")

## Why should you care?

• You probably have to rewrite all your dplyr / data.table code once you put it inside a package. I.e. working around non standard evaluation or find another way to satisfy R CMD check. And you don’t like that.
• dplyr is not respecting the class of the object it operates on; the class attribute changes on-the-fly.
• Neither dplyr nor data.table are playing nice with S4, but you really, really want a S4 data.table or tbl_df.
• You like currying as in rlist and purrr.

## Working with vectors

Inspired by rlist and purrr some low level operations on vectors are supported. The aim here is to integrate syntactic sugar for anonymous functions. Furthermore the functions should support the use of pipes.

• map and flatmap as replacements for the apply functions
• extract for subsetting
• replace for replacing elements in a vector

What we can do with map:

map(1:3, ~ .^2)
flatmap(1:3, ~ .^2)
map(1:3 ~ 11:13, c) # zip
dat <- data.frame(x = 1, y = "")
map(dat, x ~ x + 1, is.numeric)

What we can do with extract:

extract(1:10, ~ . %% 2 == 0) %>% sum
extract(1:15, ~ 15 %% . == 0)
l <- list(aList = list(x = 1), aAtomic = "hi")
extract(l, "^aL")
extract(l, is.atomic)

What we can do with replace:

replace(c(1, 2, NA), is.na, 0)
replace(c(1, 2, NA), rep(TRUE, 3), 0)
replace(c(1, 2, NA), 3, 0)
replace(list(x = 1, y = 2), "x", 0)
replace(list(x = 1, y = 2), "^x\$", 0)
replace(list(x = 1, y = "a"), is.character, NULL)