Skip to contents

ind_recode

Usage

ind_recode(
  var,
  var_prefix = "ind_",
  negator = "not",
  cat_true = NULL,
  cat_false = NULL,
  rev = FALSE
)

Arguments

var

the name of an indicator variable

var_prefix

a character string that will be ignored when creating the categorical variable

negator

a character string used to create cat_false when cat_false is NULL, default is 'not'

cat_true

a character string string to be used place of T/1/"Yes" for the categorical variable output, if NULL the category is automatically generated from the variable name

cat_false

a character string string to be used place of F/0/"No" for the categorical variable output, if NULL the category is automatically generated from the cat true and the negator

rev

logical indicating if the order should be reversed from the F/T ordering of the indicator source variable, default is FALSE

Examples

library(tibble)
library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
tibble(ind_grad = c(0,0,1,1,1 ,0 ,0)) %>%
  mutate(cat_grad  = ind_recode(ind_grad))
#> # A tibble: 7 × 2
#>   ind_grad cat_grad
#>      <dbl> <fct>   
#> 1        0 not grad
#> 2        0 not grad
#> 3        1 grad    
#> 4        1 grad    
#> 5        1 grad    
#> 6        0 not grad
#> 7        0 not grad

tibble(ind_grad = c(TRUE,TRUE,FALSE)) %>%
  mutate(cat_grad  = ind_recode(ind_grad))
#> # A tibble: 3 × 2
#>   ind_grad cat_grad
#>   <lgl>    <fct>   
#> 1 TRUE     grad    
#> 2 TRUE     grad    
#> 3 FALSE    not grad

tibble(ind_grad = c("Y", "N")) %>%
  mutate(cat_grad  = ind_recode(ind_grad))
#> # A tibble: 2 × 2
#>   ind_grad cat_grad
#>   <chr>    <fct>   
#> 1 Y        grad    
#> 2 N        not grad

tibble(ind_grad = c("y", "n")) %>%
  mutate(cat_grad  = ind_recode(ind_grad))
#> # A tibble: 2 × 2
#>   ind_grad cat_grad
#>   <chr>    <fct>   
#> 1 y        grad    
#> 2 n        not grad

tibble(ind_grad = c("yes", "no")) %>%
  mutate(cat_grad  = ind_recode(ind_grad))
#> # A tibble: 2 × 2
#>   ind_grad cat_grad
#>   <chr>    <fct>   
#> 1 yes      grad    
#> 2 no       not grad