wrapr
1.6.2
is now up on CRAN. We have some neat new features for R
users to try (in addition to many earlier wrapr
goodies).
The first is the %in_block%
alternate notation for let()
.
The wrapr
let()
-block allows easy replacement of names in name-capturing interfaces (such as transform()
), as we show below.
1 |
|
1 |
|
The qc()
notation allowed us to specify a named-vector
without quotes. qc(a = b)
is equivalent to c("a" = "b")
.
With the %in_block%
operator notation one writes the let()
-block as an in-line operator supplying the mapping into a code block. The above example can now be re-written as the following.
1 |
|
1 |
|
This notation can be handy for defining functions.
1 |
|
1 |
|
We can think of the above function definition notation as having two blocks: the alias defining block (the portion before “%in_block%
”) and the templated function body (the portion after “%in_block%
”). Notice how easy it is to use this notation to convert a non-standard (or name/code-capturing interface) into a value-oriented interface. The point is value-oriented interfaces are much more re-usable and easier to program over (use in for-loops, applies, and functions).
The second new feature is the orderv()
function, a value-oriented adapter for base::order()
. orderv()
uses a vector of column names to compute an ordering permutation for a data.frame
. We can use it as we show below.
1 |
|
1 |
|
Of course we have also have all the steps wrapped in a convenient function: sortv()
.
1 |
|
1 |
|
For details on “method = "radix"
” please see our earlier tip here.
A third new feature is mk_formula()
. mk_formula()
is used to build simple formulas for modeling tasks (which may have a large number of variables) without any string processing or parsing.
Our usual advice for building simple formulas has been to use the paste()
-based methods exhibited in “R Tip: How to Pass a formula to lm”. This remains good advice. However mk_formula()
is a more concise and more hygienic alternative. An example is given below.
1 |
|
1 |
|
1 |
|
1 |
|
The above notation is good for programming over modeling tasks.
Like this:
Like Loading…
Related