Encode data frame column using external crosswalk file.
encodefrom( .data, var, cw_file, raw, clean, label, delimiter = NULL, sheet = NULL, case_ignore = TRUE, ignore_tibble = FALSE ) encodefrom_( .data, var, cw_file, raw, clean, label, delimiter = NULL, sheet = NULL, case_ignore = TRUE, ignore_tibble = FALSE )
.data | Data frame or tbl_df |
---|---|
var | Column name of vector to be encoded |
cw_file | Either data frame object or string with path to
external crosswalk file, including path, which has columns
representing |
raw | Name of column in |
clean | Name of column in |
label | Name of column in |
delimiter | String delimiter used to parse
|
sheet | Specify sheet if |
case_ignore | Ignore case when matching current ( |
ignore_tibble | Ignore |
Vector that is either a factor or labelled, depending on data input and options
encodefrom_
: Standard evaluation version of
encodefrom
(var
, raw
, clean
,
and label
must be strings when using this version)
df <- data.frame(state = c('Kentucky','Tennessee','Virginia'), stfips = c(21,47,51), cenregnm = c('South','South','South')) df_tbl <- tibble::as_tibble(df) cw <- get(data(stcrosswalk)) df$state2 <- encodefrom(df, state, cw, stname, stfips, stabbr) df_tbl$state2 <- encodefrom(df_tbl, state, cw, stname, stfips, stabbr) df_tbl$state3 <- encodefrom(df_tbl, state, cw, stname, stfips, stabbr, ignore_tibble = TRUE) haven::as_factor(df_tbl)#> # A tibble: 3 x 5 #> state stfips cenregnm state2 state3 #> <chr> <dbl> <chr> <fct> <fct> #> 1 Kentucky 21 South KY KY #> 2 Tennessee 47 South TN TN #> 3 Virginia 51 South VA VA#> # A tibble: 3 x 5 #> state stfips cenregnm state2 state3 #> <chr> <dbl> <chr> <int> <fct> #> 1 Kentucky 21 South 21 KY #> 2 Tennessee 47 South 47 TN #> 3 Virginia 51 South 51 VA