Project Status: Active - The project has reached a stable, usable state and is being actively developed. Build Status Build status

setter: Mutators That Work With Pipes

Mutators to set attributes of variables, that work well in a pipe (much like stats::setNames).


To install the development version, you first need the devtools package.


Then you can install the setter package using



set_* functions change an attribute, then return their first input, allowing you to chain them together. For example, to turn a vector into a matrix, you could do

m <- 1:12 %>%
  set_dim(3:4) %>%
  set_dimnames(list(letters[1:3], LETTERS[1:4])) 
##   A B C  D
## a 1 4 7 10
## b 2 5 8 11
## c 3 6 9 12

To copy attributes from one variable to another, use a copy_* function. %>%
  copy_dim(x) %>%
##   A     B     C     D    
## a "Jan" "Apr" "Jul" "Oct"
## b "Feb" "May" "Aug" "Nov"
## c "Mar" "Jun" "Sep" "Dec"

set_* and copy_* functions are available for the following attributes: names, colnames, rownames, dimnames, class, dim, length.

There are also set_attributes and copy_attributes for setting/copying arbitrary attributes. Here's the previous example thrice more. copy_all_attributes and copy_most_attributes provide a shortcut for copying all the outputs, the latter using base::mostattributes<- to take special care with dim, names and dimnames. %>%
  copy_attributes(x, c("dim", "dimnames")) %>%
  copy_all_attributes(x) %>%
  copy_most_attributes(x) # special care with dim, names, dimnames

Try the setter package in your browser

Any scripts or data that you put into this service are public.

setter documentation built on May 1, 2019, 10:29 p.m.