cat(gsub("\\n   ", "", packageDescription("dat", fields = "Description")))


From GitHub




Why should you care?

A link to dplyr

The examples are from the introductory vignette of dplyr. You still work with data frames: so you can simply mix in dplyr features whenever you need them. The functions filtar, mutar and sumar are R CMD check friendly replacements for the corresponding versions in dplyr. For select you can use extract. The function names are chosen so that they are similar but do not conflict with dplyrs function names - so dplyr can be savely attached to the search path.


Select rows

filtar can be used as a replacement for filter and slice. When you reference a variable in the data itself, you can indicate this by using a one sided formula.

filtar(flights, ~ month == 1 & day == 1)
filtar(flights, 1:10)

And for sorting:

filtar(flights, ~ order(year, month, day))

Select cols

You can use characters, logicals, regular expressions and functions to select columns. Regular expressions are indicated by a leading "^". Character are simply passed to dplyr::select_.

flights %>%
  extract(c("year", "month", "day")) %>%
  extract("year:day") %>%
  extract("^day$") %>%

Operations on columns

The main difference between mutate and mutar is that you use a ~ instead of =.

  gain ~ arr_delay - dep_delay,
  speed ~ distance / air_time * 60

Grouping data is handled within mutar:

mutar(flights, n ~ n(), by = "month")
sumar(flights, delay ~ mean(dep_delay, na.rm = TRUE), by = "month")

You can also provide additional arguments to a formula. This is especially helpful when you want to pass arguments from a function to such expressions. The additional augmentation can be anything which you can use to select columns (character, regular expression, function) or a named list where each element is a character.

  .n ~ mean(.n, na.rm = TRUE) | "^.*delay$",
  x ~ mean(x, na.rm = TRUE) | list(x = "arr_time"),
  by = "month"

A link to S4

Using this package you can create S4 classes to contain a data frame (or a data.table) and use the interface to dplyr. Both dplyr and data.table do not support integration with S4. The main function here is mutar which is generic enough to link to subsetting of rows and cols as well as mutate and summarise. In the background dplyrs ability to work on a data.table is being used.


setClass("DataTable", "data.table")

DataTable <- function(...) {
  new("DataTable", data.table::data.table(...))

setMethod("[", "DataTable", mutar)

dtflights <-, nycflights13::flights)

dtflights[1:10, "year:day"]
dtflights[n ~ n(), by = "month"]
dtflights[n ~ n(), sby = "month"]

dtflights %>%
  filtar(~month > 6) %>%
  mutar(n ~ n(), by = "month") %>%
  sumar(n ~ first(n), by = "month")

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.

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

wahani/dat documentation built on Jan. 22, 2018, 10:19 p.m.