tf_try: Try multiple transformations

Description Usage Arguments Details Examples

View source: R/tf_try.R

Description

Attempt multiple transformations of a vector

Usage

1
2
3
4
5
6
7
8
tf_try(x, fun, ...)

is.tf_try(x)

## S3 method for class 'tf_try'
print(x, ...)

tf_end(x)

Arguments

x

vector

fun

function to try out on vector

...

additional arguments to pass to fun

Details

tf_try transforms the data but keep track of the original vector allowing for multiple attempts at transforming the vectors

1
2
3
4
 x %>% 
   tf_try( ... ) %>% 
   tf_try( .... ) %>% 
   tf_end()             # optional 

This is very handy for transforming data that allowed or didn't enforce a particular format such as a date.

tf_end stops the trying of values by stripping the tf_try class and stops recording the original values.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
  
 # Contrived example sqrt(-x) 
  -2:2 %>%  
    tf_try( sqrt ) %>% 
    tf_try( . %>% abs %>% sqrt )
  
 # Shift x  
  -2:2 %>%  
    tf_try( sqrt ) %>% 
    tf_try( function(x) sqrt( x+ abs(min(x)) ) ) 
 
  c(-2,1,0,NA,1,2) %>% 
    tf_try( sqrt ) %>% 
    tf_try( function(x) sqrt( x+ abs(min(x)) ) ) 
     

decisionpatterns/coercion documentation built on Nov. 4, 2019, 10:23 a.m.