seplyr is an attempt to work with the dplyr 0.7.* R package through standard evaluation interfaces with a minimum of change and minimum of cognitive friction. Beyond championing standard evaluation it attempts to introduce as few of its own opinions as possible.
A case in point is dplyr::rename()/seplyr::rename_se(). dplyr::rename() is used as follows.
suppressPackageStartupMessages(library("dplyr"))
datasets::mtcars %>%
rename(cylinders = cyl, gears = gear) %>%
head()## mpg cylinders disp hp drat wt qsec vs am gears carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Notice dplyr::rename() renamings are written as assignments making them very similar to dplyr::mutate(). This is the reverse from how R usually handles list or mapping structures. In named vectors or named lists keys are written on the left and values are written on the right as follows.
mp <- c("cyl" = "cylinders", "gear" = "gears")
print(mp)## cyl gear
## "cylinders" "gears"
Because seplyr is intended to be a set of adapters for dplyr we simply adopt dplyr::rename()’s convention. This allows the user to mechanically translate their experience and expectations from scripting over dplyr::rename() to directly use seplyr::rename_se() as follows.
library("seplyr")
datasets::mtcars %.>%
rename_se(., c("cylinders" := "cyl", "gears" := "gear")) %.>%
head(.)## mpg cylinders disp hp drat wt qsec vs am gears carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
We hope this makes it easy to translate one-off analyses into re-usable scripts by incrementally replacing known variable names with parametric versions. The := operator is just a convenience function for building up maps, we could also have written rename_se(c("cylinders" = "cyl", "gears" = "gear")) or passed in a named vector built up elsewhere.
rename_se interprets all left-hand names as new column names and all right-hand names as old column names. This allows rename_se to be used to swap columns:
data.frame(a = 1, b = 2) %.>%
rename_se(., c('a', 'b') := c('b', 'a'))## b a
## 1 1 2
Please see help("%.>%", package="wrapr") for details on “dot pipe.”