inst/vignette/rename.md

rename

library(pmtables)
vars <- dplyr::vars

Motivation

I wanted a way to capture column names as a character vector along with a second set of names for renaming those data items. The primary need was a vector of names to use rather columns to select from a data set.

Solution

new_names class with methods for character and quosure. This is two methods and the generic.

pmtables:::new_names("a,b,c")
.   a   b   c 
. "a" "b" "c"
pmtables:::new_names(c("a", "b", "c"))
.   a   b   c 
. "a" "b" "c"
pmtables:::new_names(vars(a,b,c))
.   a   b   c 
. "a" "b" "c"
pmtables:::new_names(vars(A = a, B = b, C = c))
.   A   B   C 
. "a" "b" "c"

Also accepts rename as named list

re <- list(A = "a", B = "b")

pmtables:::new_names("A,B", table = re)
.   a   b 
. "A" "B"

This is a little backward from the rename(new_name = old_name) syntax; but it is made to handle output from yspec, where things are already keyed by the existing column name. This could easily be modified to invert names / values during this lookup process.

Works with select

library(dplyr)
x <- pmtables:::new_names(vars(b = B, a = A))

x
.   b   a 
. "B" "A"
tab <- tibble(A = 1, B = 2)


tab
. # A tibble: 1 x 2
.       A     B
.   <dbl> <dbl>
. 1     1     2
select(tab,all_of(x))
. # A tibble: 1 x 2
.       b     a
.   <dbl> <dbl>
. 1     2     1

Or rename

rename(tab, all_of(x))
. # A tibble: 1 x 2
.       a     b
.   <dbl> <dbl>
. 1     1     2


metrumresearchgroup/pmtables documentation built on Oct. 27, 2024, 5:16 p.m.